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'
How to Fix the KeyError? We can simply fix the error by correcting the spelling of the key. If we are not sure about the spelling we can simply print the list of all column names and crosscheck.
You can create an empty dataframe by importing pandas from the python library. Later, using the pd. DataFrame(), create an empty dataframe without rows and columns as shown in the below example.
skipna : boolean, default True. Exclude NA/null values. If an entire row/column is NA, the result will be NA. level : int or level name, default None. If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series.
We can easily create an empty series in Pandas which means it will not have any value. The syntax that is used for creating an Empty Series: <series object> = pandas. Series()
Creating a completely empty Pandas Dataframe is very easy. We simply create a dataframe object without actually passing in any data: This returns the following: We can see from the output that the dataframe is empty.
The Pandas Dataframe is a structure that has data in the 2D format and labels with it. DataFrames are widely used in data science, machine learning, and other such places. DataFrames are the same as SQL tables or Excel sheets but these are faster in use. Attention geek!
This is only happening on the creation of an empty dataframe, is this a recently introduced bug. Show activity on this post. Show activity on this post. 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.
A better approach to append rows to empty DataFrame is by method concat (): So in order to append multiple rows to empty or full DataFrame df like: 6. Conclusion And now we're able to create empty DataFrame in several ways. We looked at how to add data, index and columns. We covered more efficient way of doing it.
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
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)
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
pandas version was the problem in my case.
It should work if you shift from
pandas==0.25.3 into pandas==1.2.3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With