Skip to content

BUG: reset_index is passing a bad dtype to NumPy #35095

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

Closed
3 tasks done
bashtage opened this issue Jul 2, 2020 · 4 comments · Fixed by #35111
Closed
3 tasks done

BUG: reset_index is passing a bad dtype to NumPy #35095

bashtage opened this issue Jul 2, 2020 · 4 comments · Fixed by #35111
Labels
Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays. Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@bashtage
Copy link
Contributor

bashtage commented Jul 2, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug **DOES NOT ** exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

index = pd.period_range("12-1-2000", periods=2, freq="Q-DEC")
df = pd.DataFrame([[1],[2]],index=index)
df = df.reset_index()

Problem description

Should reset the index.

This is happening on the nightly:

pandas: 1.1.0.dev0+2004.g8d10bfb6f

Full Travis CI log with 4 identical failures:

https://travis-ci.org/github/statsmodels/statsmodels/jobs/704310975

The relevant part of the traceback:

statsmodels/tsa/statespace/tests/test_news.py:617: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
statsmodels/tsa/statespace/tests/test_news.py:242: in check_news
    details_by_update = news.details_by_update
statsmodels/base/wrapper.py:36: in __getattribute__
    obj = getattr(results, attr)
statsmodels/tsa/statespace/news.py:392: in details_by_update
    df = df.reset_index()
venv/lib/python3.8/site-packages/pandas/core/frame.py:4800: in reset_index
    level_values = _maybe_casted_values(lev, lab)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
index = PeriodIndex(['2009Q2', '2009Q3', '2009Q4', '2010Q1'], dtype='period[Q-DEC]', name='impact date', freq='Q-DEC')
labels = array([], dtype=int8)
    def _maybe_casted_values(index, labels=None):
        values = index._values
        if not isinstance(index, (PeriodIndex, DatetimeIndex)):
            if values.dtype == np.object_:
                values = lib.maybe_convert_objects(values)
    
        # if we have the labels, extract the values with a mask
        if labels is not None:
            mask = labels == -1
    
            # we can have situations where the whole mask is -1,
            # meaning there is nothing found in labels, so make all nan's
            if mask.all():
>               values = np.empty(len(mask), dtype=index.dtype)
E               TypeError: Cannot interpret 'period[Q-DEC]' as a data type

Expected Output

    index  0
0  2000Q4  1
1  2001Q1  2

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
Python: 3.8.1.final.0
OS: Linux 5.0.0-1031-gcp #32-Ubuntu SMP Tue Feb 11 03:55:48 UTC 2020 x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
statsmodels
===========
Installed: v0.12.0.dev0+519.g502132468 (/home/travis/build/statsmodels/statsmodels/statsmodels)
Required Dependencies
=====================
cython: 3.0a5 (/home/travis/build/statsmodels/statsmodels/venv/lib/python3.8/site-packages/Cython)
numpy: 1.20.0.dev0+4d5b255 (/home/travis/build/statsmodels/statsmodels/venv/lib/python3.8/site-packages/numpy)
scipy: 1.6.0.dev0+fd6f6d6 (/home/travis/build/statsmodels/statsmodels/venv/lib/python3.8/site-packages/scipy)
pandas: 1.1.0.dev0+2004.g8d10bfb6f (/home/travis/build/statsmodels/statsmodels/venv/lib/python3.8/site-packages/pandas)
    dateutil: 2.8.1 (/home/travis/build/statsmodels/statsmodels/venv/lib/python3.8/site-packages/dateutil)
patsy: 0.5.1 (/home/travis/build/statsmodels/statsmodels/venv/lib/python3.8/site-packages/patsy)
Optional Dependencies
=====================
matplotlib: 3.3.0rc1+128.g5e11a2312 (/home/travis/build/statsmodels/statsmodels/venv/lib/python3.8/site-packages/matplotlib)
    backend: TkAgg 
cvxopt: 1.2.5 (/home/travis/build/statsmodels/statsmodels/venv/lib/python3.8/site-packages/cvxopt)
joblib: 0.16.0 (/home/travis/build/statsmodels/statsmodels/venv/lib/python3.8/site-packages/joblib)
Developer Tools
================
IPython: Not installed
    jinja2: Not installed
sphinx: Not installed
    pygments: Not installed
pytest: 5.4.3 (/home/travis/build/statsmodels/statsmodels/venv/lib/python3.8/site-packages/pytest)
virtualenv: Not installed
@bashtage bashtage added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 2, 2020
@simonjayhawkins
Copy link
Member

xref #34942 cc @rhshadrach

@simonjayhawkins simonjayhawkins added Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 2, 2020
@TomAugspurger TomAugspurger added this to the 1.1 milestone Jul 2, 2020
@simonjayhawkins
Copy link
Member

@bashtage I'm not getting a exception with the code sample, but after looking at the statsmodels tests, I can reproduce using the following.

>>> import pandas as pd
>>>
>>> pd.__version__
'1.1.0.dev0+2013.ga4e19fa52'
>>>
>>> df = pd.DataFrame(
...     index=pd.MultiIndex(levels=[[pd.Period("2009Q2")], []], codes=[[], []])
... )
>>> print(df)
Empty DataFrame
Columns: []
Index: []
>>>
>>> df.reset_index()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\simon\pandas\pandas\core\frame.py", line 4800, in reset_index
    level_values = _maybe_casted_values(lev, lab)
  File "C:\Users\simon\pandas\pandas\core\frame.py", line 4734, in _maybe_casted_values
    values = np.empty(len(mask), dtype=index.dtype)
TypeError: data type not understood
>>>

@bashtage
Copy link
Contributor Author

bashtage commented Jul 3, 2020

I made the sample code based on the traceback without actually checking it raised. Should have been more careful.

@simonjayhawkins simonjayhawkins added Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays. labels Jul 3, 2020
@rhshadrach
Copy link
Member

Thanks @bashtage and @simonjayhawkins. I'll be able to work on this come Sunday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays. Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants