Skip to content

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

@bashtage

Description

@bashtage
  • 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()

Details
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Dtype ConversionsUnexpected or buggy dtype conversionsExtensionArrayExtending pandas with custom dtypes or arrays.IndexingRelated to indexing on series/frames, not to indexes themselvesRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions