BUG: Automatic upcast to object fails when unstacking datetime/timedelta with incompatible fill_value (original) (raw)

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.