Skip to content

DataFrame constructor fails with extension dtype and columns #27953

Closed
@TomAugspurger

Description

@TomAugspurger

I think this should be supported.

In [8]: pd.DataFrame(columns=['a'], dtype=pd.CategoricalDtype())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-a44ecacab92a> in <module>
----> 1 pd.DataFrame(columns=['a'], dtype=pd.CategoricalDtype())

~/sandbox/pandas/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
    405             )
    406         elif isinstance(data, dict):
--> 407             mgr = init_dict(data, index, columns, dtype=dtype)
    408         elif isinstance(data, ma.MaskedArray):
    409             import numpy.ma.mrecords as mrecords

~/sandbox/pandas/pandas/core/internals/construction.py in init_dict(data, index, columns, dtype)
    224         # no obvious "empty" int column
    225         if missing.any() and not is_integer_dtype(dtype):
--> 226             if dtype is None or np.issubdtype(dtype, np.flexible):
    227                 # GH#1783
    228                 nan_dtype = object

~/Envs/pandas-dev/lib/python3.7/site-packages/numpy/core/numerictypes.py in issubdtype(arg1, arg2)
    391     """
    392     if not issubclass_(arg1, generic):
--> 393         arg1 = dtype(arg1).type
    394     if not issubclass_(arg2, generic):
    395         arg2_orig = arg2

TypeError: data type not understood

Expected Output

In [10]: pd.DataFrame(columns=['a']).astype('category')
Out[10]:
Empty DataFrame
Columns: [a]
Index: []

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConstructorsSeries/DataFrame/Index/pd.array ConstructorsDtype ConversionsUnexpected or buggy dtype conversionsExtensionArrayExtending pandas with custom dtypes or arrays.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions