Skip to content

Commit 2deca14

Browse files
committed
Fixed #38419 - BUG: set_index screws up the dtypes on empty DataFrames
1 parent ed8811a commit 2deca14

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

pandas/tests/frame/methods/test_set_index.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ def test_set_index_empty_column(self):
4949

5050
def test_set_index_empty_dataframe(self):
5151
# GH#38419
52-
df = DataFrame(
52+
df1 = DataFrame(
5353
{"a": Series(dtype="datetime64[ns]"), "b": Series(dtype="int64"), "c": []}
5454
)
55-
56-
expected = df.set_index(["a", "b"])
57-
tm.assert_frame_equal(df.reset_index(drop=True), expected.reset_index())
58-
assert df.empty is True
55+
56+
df2 = df1.set_index(['a', 'b'])
57+
result = df2.index.to_frame().dtypes
58+
expected = df1[['a', 'b']].dtypes
59+
tm.assert_series_equal(result, expected)
5960

6061
def test_set_index_multiindexcolumns(self):
6162
columns = MultiIndex.from_tuples([("foo", 1), ("foo", 2), ("bar", 1)])
@@ -153,9 +154,9 @@ def test_set_index_cast(self):
153154
tm.assert_frame_equal(df, df2)
154155

155156
# A has duplicate values, C does not
156-
@pytest.mark.parametrize("keys", ["A", "C", ["A", "B"], ("tuple", "as", "label")])
157-
@pytest.mark.parametrize("inplace", [True, False])
158-
@pytest.mark.parametrize("drop", [True, False])
157+
@ pytest.mark.parametrize("keys", ["A", "C", ["A", "B"], ("tuple", "as", "label")])
158+
@ pytest.mark.parametrize("inplace", [True, False])
159+
@ pytest.mark.parametrize("drop", [True, False])
159160
def test_set_index_drop_inplace(self, frame_of_index_cols, drop, inplace, keys):
160161
df = frame_of_index_cols
161162

@@ -176,8 +177,8 @@ def test_set_index_drop_inplace(self, frame_of_index_cols, drop, inplace, keys):
176177
tm.assert_frame_equal(result, expected)
177178

178179
# A has duplicate values, C does not
179-
@pytest.mark.parametrize("keys", ["A", "C", ["A", "B"], ("tuple", "as", "label")])
180-
@pytest.mark.parametrize("drop", [True, False])
180+
@ pytest.mark.parametrize("keys", ["A", "C", ["A", "B"], ("tuple", "as", "label")])
181+
@ pytest.mark.parametrize("drop", [True, False])
181182
def test_set_index_append(self, frame_of_index_cols, drop, keys):
182183
df = frame_of_index_cols
183184

@@ -193,8 +194,8 @@ def test_set_index_append(self, frame_of_index_cols, drop, keys):
193194
tm.assert_frame_equal(result, expected)
194195

195196
# A has duplicate values, C does not
196-
@pytest.mark.parametrize("keys", ["A", "C", ["A", "B"], ("tuple", "as", "label")])
197-
@pytest.mark.parametrize("drop", [True, False])
197+
@ pytest.mark.parametrize("keys", ["A", "C", ["A", "B"], ("tuple", "as", "label")])
198+
@ pytest.mark.parametrize("drop", [True, False])
198199
def test_set_index_append_to_multiindex(self, frame_of_index_cols, drop, keys):
199200
# append to existing multiindex
200201
df = frame_of_index_cols.set_index(["D"], drop=drop, append=True)
@@ -218,7 +219,7 @@ def test_set_index_after_mutation(self):
218219
# MultiIndex constructor does not work directly on Series -> lambda
219220
# Add list-of-list constructor because list is ambiguous -> lambda
220221
# also test index name if append=True (name is duplicate here for B)
221-
@pytest.mark.parametrize(
222+
@ pytest.mark.parametrize(
222223
"box",
223224
[
224225
Series,
@@ -229,10 +230,10 @@ def test_set_index_after_mutation(self):
229230
lambda x: MultiIndex.from_arrays([x]),
230231
],
231232
)
232-
@pytest.mark.parametrize(
233+
@ pytest.mark.parametrize(
233234
"append, index_name", [(True, None), (True, "B"), (True, "test"), (False, None)]
234235
)
235-
@pytest.mark.parametrize("drop", [True, False])
236+
@ pytest.mark.parametrize("drop", [True, False])
236237
def test_set_index_pass_single_array(
237238
self, frame_of_index_cols, drop, append, index_name, box
238239
):
@@ -261,14 +262,14 @@ def test_set_index_pass_single_array(
261262

262263
# MultiIndex constructor does not work directly on Series -> lambda
263264
# also test index name if append=True (name is duplicate here for A & B)
264-
@pytest.mark.parametrize(
265+
@ pytest.mark.parametrize(
265266
"box", [Series, Index, np.array, list, lambda x: MultiIndex.from_arrays([x])]
266267
)
267-
@pytest.mark.parametrize(
268+
@ pytest.mark.parametrize(
268269
"append, index_name",
269270
[(True, None), (True, "A"), (True, "B"), (True, "test"), (False, None)],
270271
)
271-
@pytest.mark.parametrize("drop", [True, False])
272+
@ pytest.mark.parametrize("drop", [True, False])
272273
def test_set_index_pass_arrays(
273274
self, frame_of_index_cols, drop, append, index_name, box
274275
):
@@ -292,7 +293,7 @@ def test_set_index_pass_arrays(
292293
# MultiIndex constructor does not work directly on Series -> lambda
293294
# We also emulate a "constructor" for the label -> lambda
294295
# also test index name if append=True (name is duplicate here for A)
295-
@pytest.mark.parametrize(
296+
@ pytest.mark.parametrize(
296297
"box2",
297298
[
298299
Series,
@@ -304,7 +305,7 @@ def test_set_index_pass_arrays(
304305
lambda x: x.name,
305306
],
306307
)
307-
@pytest.mark.parametrize(
308+
@ pytest.mark.parametrize(
308309
"box1",
309310
[
310311
Series,
@@ -316,10 +317,10 @@ def test_set_index_pass_arrays(
316317
lambda x: x.name,
317318
],
318319
)
319-
@pytest.mark.parametrize(
320+
@ pytest.mark.parametrize(
320321
"append, index_name", [(True, None), (True, "A"), (True, "test"), (False, None)]
321322
)
322-
@pytest.mark.parametrize("drop", [True, False])
323+
@ pytest.mark.parametrize("drop", [True, False])
323324
def test_set_index_pass_arrays_duplicate(
324325
self, frame_of_index_cols, drop, append, index_name, box1, box2
325326
):
@@ -352,8 +353,8 @@ def test_set_index_pass_arrays_duplicate(
352353
expected = expected.set_index([keys[1]], drop=drop, append=True)
353354
tm.assert_frame_equal(result, expected)
354355

355-
@pytest.mark.parametrize("append", [True, False])
356-
@pytest.mark.parametrize("drop", [True, False])
356+
@ pytest.mark.parametrize("append", [True, False])
357+
@ pytest.mark.parametrize("drop", [True, False])
357358
def test_set_index_pass_multiindex(self, frame_of_index_cols, drop, append):
358359
df = frame_of_index_cols
359360
keys = MultiIndex.from_arrays([df["A"], df["B"]], names=["A", "B"])
@@ -515,8 +516,8 @@ def test_set_index_verify_integrity(self, frame_of_index_cols):
515516
with pytest.raises(ValueError, match="Index has duplicate keys"):
516517
df.set_index([df["A"], df["A"]], verify_integrity=True)
517518

518-
@pytest.mark.parametrize("append", [True, False])
519-
@pytest.mark.parametrize("drop", [True, False])
519+
@ pytest.mark.parametrize("append", [True, False])
520+
@ pytest.mark.parametrize("drop", [True, False])
520521
def test_set_index_raise_keys(self, frame_of_index_cols, drop, append):
521522
df = frame_of_index_cols
522523

@@ -537,9 +538,9 @@ def test_set_index_raise_keys(self, frame_of_index_cols, drop, append):
537538
with pytest.raises(KeyError, match=msg):
538539
df.set_index(["A", df["A"], tuple(df["A"])], drop=drop, append=append)
539540

540-
@pytest.mark.parametrize("append", [True, False])
541-
@pytest.mark.parametrize("drop", [True, False])
542-
@pytest.mark.parametrize("box", [set], ids=["set"])
541+
@ pytest.mark.parametrize("append", [True, False])
542+
@ pytest.mark.parametrize("drop", [True, False])
543+
@ pytest.mark.parametrize("box", [set], ids=["set"])
543544
def test_set_index_raise_on_type(self, frame_of_index_cols, box, drop, append):
544545
df = frame_of_index_cols
545546

@@ -553,14 +554,14 @@ def test_set_index_raise_on_type(self, frame_of_index_cols, box, drop, append):
553554
df.set_index(["A", df["A"], box(df["A"])], drop=drop, append=append)
554555

555556
# MultiIndex constructor does not work directly on Series -> lambda
556-
@pytest.mark.parametrize(
557+
@ pytest.mark.parametrize(
557558
"box",
558559
[Series, Index, np.array, iter, lambda x: MultiIndex.from_arrays([x])],
559560
ids=["Series", "Index", "np.array", "iter", "MultiIndex"],
560561
)
561-
@pytest.mark.parametrize("length", [4, 6], ids=["too_short", "too_long"])
562-
@pytest.mark.parametrize("append", [True, False])
563-
@pytest.mark.parametrize("drop", [True, False])
562+
@ pytest.mark.parametrize("length", [4, 6], ids=["too_short", "too_long"])
563+
@ pytest.mark.parametrize("append", [True, False])
564+
@ pytest.mark.parametrize("drop", [True, False])
564565
def test_set_index_raise_on_len(
565566
self, frame_of_index_cols, box, length, drop, append
566567
):

0 commit comments

Comments
 (0)