AttributeError: 'DataFrame' object has no attribute 'infer_objects'

Viewed 6698

Every time I try the method infer_objects(), even when following the official doc (link), I got this error:

AttributeError: 'DataFrame' object has no attribute 'infer_objects'

Example of code:

import pandas as pd
df = pd.DataFrame({"A": ["a", 1, 2, 3]})
df = df.iloc[1:]
df = df.infer_objects()

Why do I get this error?

1 Answers

I can support Jon Clements answer and F. Varlets issue: updating pandas works

To avoid convert_objects and infer_objects:

setting datatypes manually:

In [21]: df=pd.DataFrame([['a','1'],['b','2']], columns=['x','y'])

In [22]: df.dtypes
Out[22]: 
x    object
y    object
dtype: object

In [23]: for k in {'x':'object','y':'int'}:
    ...:     df[k]=pd.to_numeric(df[k], errors='ignore')
    ...:     

In [24]: df.dtypes
Out[24]: 
x    object
y     int64
dtype: object

automatic datatype convertion:

In [10]: df=pd.DataFrame([['a','1'],['b','2']], columns=['x','y'])

In [11]: df.dtypes
Out[11]: 
x    object
y    object
dtype: object

In [12]: for k in list(df):
    ...:    ...:     df[k]=pd.to_numeric(df[k], errors='ignore')
    ...:    

In [13]: df.dtypes
Out[13]: 
x    object
y     int64
dtype: object
Related