Skip to content

Commit 6ea8474

Browse files
REGR: Dataframe.reset_index() on empty DataFrame with MI and datatime level (#35673)
1 parent 59febbd commit 6ea8474

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

doc/source/whatsnew/v1.1.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed regressions
2222
- Fixed regression in :meth:`DataFrame.shift` with ``axis=1`` and heterogeneous dtypes (:issue:`35488`)
2323
- Fixed regression in ``.groupby(..).rolling(..)`` where a segfault would occur with ``center=True`` and an odd number of values (:issue:`35552`)
2424
- Fixed regression in :meth:`DataFrame.apply` where functions that altered the input in-place only operated on a single row (:issue:`35462`)
25+
- Fixed regression in :meth:`DataFrame.reset_index` would raise a ``ValueError`` on empty :class:`DataFrame` with a :class:`MultiIndex` with a ``datetime64`` dtype level (:issue:`35606`, :issue:`35657`)
2526
- Fixed regression where :meth:`DataFrame.merge_asof` would raise a ``UnboundLocalError`` when ``left_index`` , ``right_index`` and ``tolerance`` were set (:issue:`35558`)
2627
- Fixed regression in ``.groupby(..).rolling(..)`` where a custom ``BaseIndexer`` would be ignored (:issue:`35557`)
2728

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4816,7 +4816,7 @@ def _maybe_casted_values(index, labels=None):
48164816

48174817
# we can have situations where the whole mask is -1,
48184818
# meaning there is nothing found in labels, so make all nan's
4819-
if mask.all():
4819+
if mask.size > 0 and mask.all():
48204820
dtype = index.dtype
48214821
fill_value = na_value_for_dtype(dtype)
48224822
values = construct_1d_arraylike_from_scalar(

pandas/tests/frame/methods/test_reset_index.py

+30
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,33 @@ def test_reset_index_dtypes_on_empty_frame_with_multiindex(array, dtype):
318318
result = DataFrame(index=idx)[:0].reset_index().dtypes
319319
expected = Series({"level_0": np.int64, "level_1": np.float64, "level_2": dtype})
320320
tm.assert_series_equal(result, expected)
321+
322+
323+
def test_reset_index_empty_frame_with_datetime64_multiindex():
324+
# https://github.com/pandas-dev/pandas/issues/35606
325+
idx = MultiIndex(
326+
levels=[[pd.Timestamp("2020-07-20 00:00:00")], [3, 4]],
327+
codes=[[], []],
328+
names=["a", "b"],
329+
)
330+
df = DataFrame(index=idx, columns=["c", "d"])
331+
result = df.reset_index()
332+
expected = DataFrame(
333+
columns=list("abcd"), index=RangeIndex(start=0, stop=0, step=1)
334+
)
335+
expected["a"] = expected["a"].astype("datetime64[ns]")
336+
expected["b"] = expected["b"].astype("int64")
337+
tm.assert_frame_equal(result, expected)
338+
339+
340+
def test_reset_index_empty_frame_with_datetime64_multiindex_from_groupby():
341+
# https://github.com/pandas-dev/pandas/issues/35657
342+
df = DataFrame(dict(c1=[10.0], c2=["a"], c3=pd.to_datetime("2020-01-01")))
343+
df = df.head(0).groupby(["c2", "c3"])[["c1"]].sum()
344+
result = df.reset_index()
345+
expected = DataFrame(
346+
columns=["c2", "c3", "c1"], index=RangeIndex(start=0, stop=0, step=1)
347+
)
348+
expected["c3"] = expected["c3"].astype("datetime64[ns]")
349+
expected["c1"] = expected["c1"].astype("float64")
350+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)