Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataFrame constructor fails with extension dtype and columns #27953

Closed
TomAugspurger opened this issue Aug 16, 2019 · 0 comments · Fixed by #33651
Closed

DataFrame constructor fails with extension dtype and columns #27953

TomAugspurger opened this issue Aug 16, 2019 · 0 comments · Fixed by #33651
Labels
Constructors Series/DataFrame/Index/pd.array Constructors Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays.
Milestone

Comments

@TomAugspurger
Copy link
Contributor

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: []
@TomAugspurger TomAugspurger added Constructors Series/DataFrame/Index/pd.array Constructors Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays. labels Aug 16, 2019
@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Aug 16, 2019
@jreback jreback modified the milestones: Contributions Welcome, 1.1 May 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Constructors Series/DataFrame/Index/pd.array Constructors Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
2 participants