Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Error for creating an emptydataframe

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

like image 505
Shivangi Singh Avatar asked Feb 01 '21 19:02

Shivangi Singh


People also ask

How do I fix pandas key error?

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.

How do you create an empty data frame?

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.

What is Skipna in pandas?

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.

How can you create an empty series in pandas?

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()

How to create a completely empty pandas Dataframe?

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.

What is a pandas Dataframe?

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!

Why is my Dataframe empty after creation?

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.

How to append rows to empty Dataframe in Python?

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.


4 Answers

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

like image 110
Shivangi Singh Avatar answered Oct 27 '22 15:10

Shivangi Singh


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)
like image 39
Sandertjuhh Avatar answered Oct 27 '22 16:10

Sandertjuhh


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

like image 4
Gil Ashkenazi Avatar answered Oct 27 '22 15:10

Gil Ashkenazi


pandas version was the problem in my case.

It should work if you shift from

pandas==0.25.3 into pandas==1.2.3

like image 2
kunjung sherpa Avatar answered Oct 27 '22 14:10

kunjung sherpa