Skip to content

Commit 5e72db0

Browse files
Backport PR pandas-dev#40924: BUG: concat with DTI and all-None Index (pandas-dev#40942)
Co-authored-by: jbrockmendel <[email protected]>
1 parent 583a51a commit 5e72db0

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v1.2.5.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ including other versions of pandas.
1414

1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
17-
17+
- Regression in :func:`concat` between two :class:`DataFrames` where one has an :class:`Index` that is all-None and the other is :class:`DatetimeIndex` incorrectly raising (:issue:`40841`)
1818
-
1919
-
2020

pandas/core/indexes/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2742,7 +2742,8 @@ def _union(self, other, sort):
27422742
# worth making this faster? a very unusual case
27432743
value_set = set(lvals)
27442744
result.extend([x for x in rvals if x not in value_set])
2745-
result = Index(result)._values # do type inference here
2745+
# If objects are unorderable, we must have object dtype.
2746+
return np.array(result, dtype=object)
27462747
else:
27472748
# find indexes of things in "other" that are not in "self"
27482749
if self.is_unique:

pandas/tests/reshape/concat/test_concat.py

+20
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,23 @@ def test_concat_repeated_keys(keys, integrity):
572572
tuples = list(zip(keys, ["a", "b", "c"]))
573573
expected = Series([1, 2, 3], index=MultiIndex.from_tuples(tuples))
574574
tm.assert_series_equal(result, expected)
575+
576+
577+
def test_concat_null_object_with_dti():
578+
# GH#40841
579+
dti = pd.DatetimeIndex(
580+
["2021-04-08 21:21:14+00:00"], dtype="datetime64[ns, UTC]", name="Time (UTC)"
581+
)
582+
right = DataFrame(data={"C": [0.5274]}, index=dti)
583+
584+
idx = Index([None], dtype="object", name="Maybe Time (UTC)")
585+
left = DataFrame(data={"A": [None], "B": [np.nan]}, index=idx)
586+
587+
result = concat([left, right], axis="columns")
588+
589+
exp_index = Index([None, dti[0]], dtype=object)
590+
expected = DataFrame(
591+
{"A": [None, None], "B": [np.nan, np.nan], "C": [np.nan, 0.5274]},
592+
index=exp_index,
593+
)
594+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)