Skip to content

BUG: lib.infer_type broken for IntervalArray / PeriodArray #23553

Closed
@h-vetinari

Description

@h-vetinari

While working on #23167, I found a regression in IntervalArray / PeriodArray - I'm guessing it's related to the whole EA effort. @jorisvandenbossche @TomAugspurger

>>> import pandas as pd
>>> import pandas._libs.lib as lib
>>> s = pd.Series([pd.Period(2013), pd.Period(2018)], dtype='category')
>>> lib.infer_dtype(s.values.categories)
Traceback (most recent call last):
  File "pandas\_libs\lib.pyx", line 1166, in pandas._libs.lib.infer_dtype
    values = getattr(value, '_values', getattr(value, 'values', value))
TypeError: Cannot convert PeriodArray to numpy.ndarray
>>>
>>> t = pd.Series([pd.Interval(0, 1), pd.Interval(0, 2)], dtype='category')
>>> lib.infer_dtype(t.values.categories)
Traceback (most recent call last):
  File "pandas\_libs\lib.pyx", line 1166, in pandas._libs.lib.infer_dtype
    values = getattr(value, '_values', getattr(value, 'values', value))
TypeError: Cannot convert IntervalArray to numpy.ndarray

This was still working in v0.23.4:

>>> import pandas as pd
>>> import pandas._libs.lib as lib
>>> pd.__version__
'0.23.4'
>>> s = pd.Series([pd.Period(2013), pd.Period(2018)], dtype='category')
>>> lib.infer_dtype(s.values.categories)
'period'
>>>
>>> t = pd.Series([pd.Interval(0, 1), pd.Interval(0, 2)], dtype='category')
>>> lib.infer_dtype(t.values.categories)
'interval'

This is due to the fact that lib.infer_dtype tries
values = getattr(value, '_values', getattr(value, 'values', value))
and both IntervalArray / PeriodArray do not return a numpy array for _values anymore. For PeriodArray, it would work if _values and values were switched, but for IntervalArray, both _values and values return the same (non-numpy-array) result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDtype ConversionsUnexpected or buggy dtype conversionsExtensionArrayExtending pandas with custom dtypes or arrays.RegressionFunctionality 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