42

I tried making a dummy dataframe,

column_names = ["a", "b", "c"]

df = pd.DataFrame(columns = column_names)

I am getting the following error, this was not happening before, am I missing something. This is only happening on the creation of an empty dataframe, is this a recently introduced bug.

  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/pandas/core/frame.py", line 411, in __init__
    mgr = init_dict(data, index, columns, dtype=dtype)
  File "/usr/local/lib/python3.7/site-packages/pandas/core/internals/construction.py", line 242, in init_dict
    val = construct_1d_arraylike_from_scalar(np.nan, len(index), nan_dtype)
  File "/usr/local/lib/python3.7/site-packages/pandas/core/dtypes/cast.py", line 1221, in construct_1d_arraylike_from_scalar
    dtype = dtype.dtype
AttributeError: type object 'object' has no attribute 'dtype'

Sample code

Shivangi Singh
  • 1,001
  • 2
  • 11
  • 20

4 Answers4

43

This was happening with pandas==0.25.3 Updated to the latest pandas==1.2.1

UPDATE: This was due to a numpy package 1.20.0, so I locked the numpy package instead, numpy==1.19.5, pandas==0.25.3

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Shivangi Singh
  • 1,001
  • 2
  • 11
  • 20
37

If for some reason you don't/can't upgrade numpy/pandas, an alternative way to fix this is by specifiying the dtype when creating the DataFrame. For example:

column_names = ["a", "b", "c"]
df = pd.DataFrame(columns = column_names, dtype=object)
sander
  • 1,340
  • 1
  • 10
  • 20
5

happening due to numpy==1.20.0, and resolve with numpy==1.19.5. pandas version may not relevant, in my case, pandas==1.0.4

2

pandas version was the problem in my case.

It should work if you shift from

pandas==0.25.3 into pandas==1.2.3

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
kunjung sherpa
  • 181
  • 1
  • 4