Pandas DataFrame iloc spoils data type

Having pandas 0.19.2.

Here is an example:

testdf = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [1.0, 2.0, 3.0, 4.0]})
testdf.dtypes

Conclusion:

A      int64
B    float64
dtype: object

Now everything looks great, but I don’t like that (first call - pd.Series.ilocand second - pd.DataFrame.iloc)

print(type(testdf.A.iloc[0]))
print(type(testdf.iloc[0].A))

Conclusion:

<class 'numpy.int64'>
<class 'numpy.float64'>

I found it, trying to understand why the operation pd.DataFrame.join()almost does not intersect with two columns int64, although there should be a lot of them. I assume that due to the inconsistency of types that may be associated with this behavior, but I'm not sure ... My short investigation showed above, and now I'm a little confused.

If someone knows how to solve it, I will be very grateful for any tips!

UPD

Thanks to @EdChum for comments. So here is an example with my generated data and combining / combining behavior

testdf.join(testdf, on='A', rsuffix='3')

    A   B   A3  B3 
0   1   1.0 2.0 2.0
1   2   2.0 3.0 3.0
2   3   3.0 4.0 4.0
3   4   4.0 NaN NaN

And what is considered exactly the same pd.merge(left=testdf, right=testdf, on='A') returns

    A   B_x B_y
0   1   1.0 1.0
1   2   2.0 2.0
2   3   3.0 3.0
3   4   4.0 4.0

UPD2 @EdChum join merge. , A.join(B, on='C') A B['C'], join . , .

+4
1

, . pandas dtypes . testdf.iloc[0], pandas . . . .

, , pandas loc iloc, , __getitem__

testdf int

testdf = pd.DataFrame({'A': [1, 2, 3, 4]})

print(type(testdf.iloc[0].A))
print(type(testdf.A.iloc[0]))

<class 'numpy.int64'>
<class 'numpy.int64'>

OP

testdf = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [1.0, 2.0, 3.0, 4.0]})

print(type(testdf.iloc[0].A))
print(type(testdf.A.iloc[0]))

<class 'numpy.float64'>
<class 'numpy.int64'>

print(type(testdf.loc[0, 'A']))
print(type(testdf.iloc[0, 0]))
print(type(testdf.at[0, 'A']))
print(type(testdf.iat[0, 0]))
print(type(testdf.get_value(0, 'A')))

<class 'numpy.float64'>
<class 'numpy.float64'>
<class 'numpy.int64'>
<class 'numpy.int64'>
<class 'numpy.int64'>

, , , pandas loc iloc, , . , , loc iloc at, iat, get_value , iloc loc dataframe . at, iat get_value .


testdf.loc[0, 'A'] = 10

print(type(testdf.at[0, 'A']))

loc, pandas , dtype .

+2

Source: https://habr.com/ru/post/1666839/


All Articles