Skip to content

Commit b91e30c

Browse files
fix more tests
1 parent 8efec31 commit b91e30c

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

pandas/core/indexes/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6397,6 +6397,18 @@ def _find_common_type_compat(self, target) -> DtypeObj:
63976397
if len(self) == 0 and (
63986398
isinstance(self, RangeIndex) or self.dtype == np.object_
63996399
):
6400+
if target_dtype.kind == "M":
6401+
if hasattr(target_dtype, "tz"):
6402+
target_dtype_ns = DatetimeTZDtype("ns", tz=target_dtype.tz)
6403+
else:
6404+
target_dtype_ns = np.dtype("datetime64[ns]")
6405+
try:
6406+
Index(target, dtype=target_dtype_ns, copy=False)
6407+
except OutOfBoundsDatetime:
6408+
return np.dtype(object)
6409+
except Exception:
6410+
pass
6411+
return target_dtype_ns
64006412
return target_dtype
64016413
if (
64026414
isinstance(target, Index)

pandas/tests/frame/indexing/test_coercion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_26395(indexer_al):
113113
expected = DataFrame(
114114
{"D": [0, 0, 44.5]},
115115
index=["A", "B", "C"],
116-
columns=pd.Index(["D"], dtype=object),
116+
columns=["D"],
117117
dtype=np.float64,
118118
)
119119
tm.assert_frame_equal(df, expected)
@@ -125,7 +125,7 @@ def test_26395(indexer_al):
125125
expected = DataFrame(
126126
{"D": [0, 0, "hello"]},
127127
index=["A", "B", "C"],
128-
columns=pd.Index(["D"], dtype=object),
128+
columns=["D"],
129129
dtype=object,
130130
)
131131
tm.assert_frame_equal(df, expected)

pandas/tests/indexes/test_setops.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_union_same_types(index):
6464
assert idx1.union(idx2).dtype == idx1.dtype
6565

6666

67-
def test_union_different_types(index_flat, index_flat2, request):
67+
def test_union_different_types(index_flat, index_flat2, request, using_infer_string):
6868
# This test only considers combinations of indices
6969
# GH 23525
7070
idx1 = index_flat
@@ -93,6 +93,13 @@ def test_union_different_types(index_flat, index_flat2, request):
9393
request.applymarker(mark)
9494

9595
common_dtype = find_common_type([idx1.dtype, idx2.dtype])
96+
if using_infer_string:
97+
if len(idx1) == 0 and (idx1.dtype.kind == "O" or isinstance(idx1, RangeIndex)):
98+
common_dtype = idx2.dtype
99+
elif len(idx2) == 0 and (
100+
idx2.dtype.kind == "O" or isinstance(idx2, RangeIndex)
101+
):
102+
common_dtype = idx1.dtype
96103

97104
warn = None
98105
msg = "'<' not supported between"

pandas/tests/resample/test_base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from pandas import (
1010
DataFrame,
1111
DatetimeIndex,
12-
Index,
1312
MultiIndex,
1413
NaT,
1514
PeriodIndex,
@@ -293,7 +292,7 @@ def test_resample_count_empty_dataframe(freq, empty_frame_dti):
293292

294293
index = _asfreq_compat(empty_frame_dti.index, freq)
295294

296-
expected = DataFrame(dtype="int64", index=index, columns=Index(["a"], dtype=object))
295+
expected = DataFrame(dtype="int64", index=index, columns=["a"])
297296

298297
tm.assert_frame_equal(result, expected)
299298

0 commit comments

Comments
 (0)