Skip to content

Commit e809ac2

Browse files
only for empty + fix tests
1 parent 386c421 commit e809ac2

File tree

16 files changed

+60
-82
lines changed

16 files changed

+60
-82
lines changed

pandas/core/indexes/base.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6236,15 +6236,22 @@ def _find_common_type_compat(self, target) -> DtypeObj:
62366236
Implementation of find_common_type that adjusts for Index-specific
62376237
special cases.
62386238
"""
6239-
# breakpoint()
62406239
target_dtype, _ = infer_dtype_from(target)
62416240

62426241
if using_string_dtype():
6242+
# special case: if left or right is a zero-length RangeIndex or
6243+
# Index[object], those can be created by the default empty constructors
6244+
# -> for that case ignore this dtype and always return the other
62436245
from pandas.core.indexes.range import RangeIndex
62446246

6245-
if len(self) == 0 or self.isna().all():
6246-
if isinstance(self, RangeIndex) or self.dtype == np.object_:
6247-
return target_dtype
6247+
if len(self) == 0 and (
6248+
isinstance(self, RangeIndex) or self.dtype == np.object_
6249+
):
6250+
return target_dtype
6251+
if len(target) == 0 and (
6252+
isinstance(target, RangeIndex) or target_dtype == np.object_
6253+
):
6254+
return self.dtype
62486255

62496256
# special case: if one dtype is uint64 and the other a signed int, return object
62506257
# See https://github.com/pandas-dev/pandas/issues/26778 for discussion

pandas/tests/frame/indexing/test_coercion.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,7 @@ def test_26395(indexer_al):
8888
df["D"] = 0
8989

9090
indexer_al(df)["C", "D"] = 2
91-
expected = DataFrame(
92-
{"D": [0, 0, 2]},
93-
index=["A", "B", "C"],
94-
columns=pd.Index(["D"], dtype=object),
95-
dtype=np.int64,
96-
)
91+
expected = DataFrame({"D": [0, 0, 2]}, index=["A", "B", "C"], dtype=np.int64)
9792
tm.assert_frame_equal(df, expected)
9893

9994
with pytest.raises(TypeError, match="Invalid value"):

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@ def test_loc_setitem_datetimelike_with_inference(self):
11381138
result = df.dtypes
11391139
expected = Series(
11401140
[np.dtype("timedelta64[ns]")] * 6 + [np.dtype("datetime64[ns]")] * 2,
1141-
index=Index(list("ABCDEFGH"), dtype=object),
1141+
index=list("ABCDEFGH"),
11421142
)
11431143
tm.assert_series_equal(result, expected)
11441144

pandas/tests/frame/indexing/test_insert.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ def test_insert_with_columns_dups(self):
6868
df.insert(0, "A", ["d", "e", "f"], allow_duplicates=True)
6969
df.insert(0, "A", ["a", "b", "c"], allow_duplicates=True)
7070
exp = DataFrame(
71-
[["a", "d", "g"], ["b", "e", "h"], ["c", "f", "i"]],
72-
columns=Index(["A", "A", "A"], dtype=object),
71+
[["a", "d", "g"], ["b", "e", "h"], ["c", "f", "i"]], columns=["A", "A", "A"]
7372
)
7473
tm.assert_frame_equal(df, exp)
7574

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ def test_setitem_empty_columns(self):
150150
df["X"] = df.index
151151
df["X"] = ["x", "y", "z"]
152152
exp = DataFrame(
153-
data={"X": ["x", "y", "z"]},
154-
index=["A", "B", "C"],
155-
columns=Index(["X"], dtype=object),
153+
data={"X": ["x", "y", "z"]}, index=["A", "B", "C"], columns=["X"]
156154
)
157155
tm.assert_frame_equal(df, exp)
158156

@@ -169,9 +167,7 @@ def test_setitem_timestamp_empty_columns(self):
169167
df["now"] = Timestamp("20130101", tz="UTC")
170168

171169
expected = DataFrame(
172-
[[Timestamp("20130101", tz="UTC")]] * 3,
173-
index=range(3),
174-
columns=Index(["now"], dtype=object),
170+
[[Timestamp("20130101", tz="UTC")]] * 3, index=range(3), columns=["now"]
175171
)
176172
tm.assert_frame_equal(df, expected)
177173

@@ -210,7 +206,7 @@ def test_setitem_period_preserves_dtype(self):
210206
result = DataFrame([])
211207
result["a"] = data
212208

213-
expected = DataFrame({"a": data}, columns=Index(["a"], dtype=object))
209+
expected = DataFrame({"a": data}, columns=["a"])
214210

215211
tm.assert_frame_equal(result, expected)
216212

@@ -930,7 +926,7 @@ def test_setitem_scalars_no_index(self):
930926
# GH#16823 / GH#17894
931927
df = DataFrame()
932928
df["foo"] = 1
933-
expected = DataFrame(columns=Index(["foo"], dtype=object)).astype(np.int64)
929+
expected = DataFrame(columns=["foo"]).astype(np.int64)
934930
tm.assert_frame_equal(df, expected)
935931

936932
def test_setitem_newcol_tuple_key(self, float_frame):

pandas/tests/frame/methods/test_dropna.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import numpy as np
55
import pytest
66

7-
from pandas._config import using_string_dtype
8-
97
import pandas as pd
108
from pandas import (
119
DataFrame,
@@ -184,7 +182,6 @@ def test_dropna_multiple_axes(self):
184182
with pytest.raises(TypeError, match="supplying multiple axes"):
185183
inp.dropna(how="all", axis=(0, 1), inplace=True)
186184

187-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
188185
def test_dropna_tz_aware_datetime(self):
189186
# GH13407
190187
df = DataFrame()

pandas/tests/frame/methods/test_reset_index.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import numpy as np
55
import pytest
66

7-
from pandas._config import using_string_dtype
8-
97
from pandas.core.dtypes.common import (
108
is_float_dtype,
119
is_integer_dtype,
@@ -644,7 +642,6 @@ def test_rest_index_multiindex_categorical_with_missing_values(self, codes):
644642
tm.assert_frame_equal(res, expected)
645643

646644

647-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string) - GH#60338")
648645
@pytest.mark.parametrize(
649646
"array, dtype",
650647
[

pandas/tests/groupby/test_groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ def test_groupby_2d_malformed():
12781278
d["label"] = ["l1", "l2"]
12791279
tmp = d.groupby(["group"]).mean(numeric_only=True)
12801280
res_values = np.array([[0.0, 1.0], [0.0, 1.0]])
1281-
tm.assert_index_equal(tmp.columns, Index(["zeros", "ones"], dtype=object))
1281+
tm.assert_index_equal(tmp.columns, Index(["zeros", "ones"]))
12821282
tm.assert_numpy_array_equal(tmp.values, res_values)
12831283

12841284

pandas/tests/indexes/base_class/test_setops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def test_tuple_union_bug(self, method, expected, sort):
240240
def test_union_name_preservation(
241241
self, first_list, second_list, first_name, second_name, expected_name, sort
242242
):
243-
expected_dtype = object if not first_list or not second_list else "str"
243+
# expected_dtype = object if not first_list or not second_list else "str"
244244
first = Index(first_list, name=first_name)
245245
second = Index(second_list, name=second_name)
246246
union = first.union(second, sort=sort)
@@ -251,7 +251,7 @@ def test_union_name_preservation(
251251
expected = Index(sorted(vals), name=expected_name)
252252
tm.assert_index_equal(union, expected)
253253
else:
254-
expected = Index(vals, name=expected_name, dtype=expected_dtype)
254+
expected = Index(vals, name=expected_name)
255255
tm.assert_index_equal(union.sort_values(), expected.sort_values())
256256

257257
@pytest.mark.parametrize(

pandas/tests/indexes/test_setops.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,14 @@ def test_setop_with_categorical(index_flat, sort, method):
539539

540540
result = getattr(index, method)(other, sort=sort)
541541
expected = getattr(index, method)(index, sort=sort)
542+
if index.empty and method in ("union", "symmetric_difference"):
543+
expected = expected.astype("category")
542544
tm.assert_index_equal(result, expected, exact=exact)
543545

544546
result = getattr(index, method)(other[:5], sort=sort)
545547
expected = getattr(index, method)(index[:5], sort=sort)
548+
if index.empty and method in ("union", "symmetric_difference"):
549+
expected = expected.astype("category")
546550
tm.assert_index_equal(result, expected, exact=exact)
547551

548552

0 commit comments

Comments
 (0)