Skip to content

BUG: Automatic upcast to object fails when unstacking datetime/timedelta with incompatible fill_value #12189

@amcpherson

Description

@amcpherson

Converting to object before then unstack works, whereas automatic upcast does not happen. Missing values are replaced with NaT instead of automatically upcasting to object and filling with the requested value.

In [44]: td = [Timedelta(days=i) for i in range(4)]
In [45]: data = Series(td)
In [46]: data.index = MultiIndex.from_tuples(
   ....:    [('x', 'a'), ('x', 'b'), ('y', 'b'), ('z', 'a')])
In [47]: data.unstack(fill_value='foo')
Out[47]:
       a      b
x 0 days 1 days
y    NaT 2 days
z 3 days    NaT
In [48]: data.astype(np.object_).unstack(fill_value='foo')
Out[48]:
                 a                b
x  0 days 00:00:00  1 days 00:00:00
y              foo  2 days 00:00:00
z  3 days 00:00:00              foo

Culprit is some outdated code in _maybe_promote.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeDtype ConversionsUnexpected or buggy dtype conversionsError ReportingIncorrect or improved errors from pandasReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions