Skip to content

Commit f46d614

Browse files
jbrockmendelJulianWgs
authored andcommitted
BUG: concat with DTI and all-None Index (pandas-dev#40924)
1 parent 34466f9 commit f46d614

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
@@ -2977,7 +2977,8 @@ def _union(self, other: Index, sort):
29772977
# worth making this faster? a very unusual case
29782978
value_set = set(lvals)
29792979
value_list.extend([x for x in rvals if x not in value_set])
2980-
return Index(value_list)._values # do type inference here
2980+
# If objects are unorderable, we must have object dtype.
2981+
return np.array(value_list, dtype=object)
29812982

29822983
elif not other.is_unique and not self.is_unique:
29832984
# self and other both have duplicates

pandas/tests/reshape/concat/test_concat.py

+20
Original file line numberDiff line numberDiff line change
@@ -607,3 +607,23 @@ def test_concat_repeated_keys(keys, integrity):
607607
tuples = list(zip(keys, ["a", "b", "c"]))
608608
expected = Series([1, 2, 3], index=MultiIndex.from_tuples(tuples))
609609
tm.assert_series_equal(result, expected)
610+
611+
612+
def test_concat_null_object_with_dti():
613+
# GH#40841
614+
dti = pd.DatetimeIndex(
615+
["2021-04-08 21:21:14+00:00"], dtype="datetime64[ns, UTC]", name="Time (UTC)"
616+
)
617+
right = DataFrame(data={"C": [0.5274]}, index=dti)
618+
619+
idx = Index([None], dtype="object", name="Maybe Time (UTC)")
620+
left = DataFrame(data={"A": [None], "B": [np.nan]}, index=idx)
621+
622+
result = concat([left, right], axis="columns")
623+
624+
exp_index = Index([None, dti[0]], dtype=object)
625+
expected = DataFrame(
626+
{"A": [None, None], "B": [np.nan, np.nan], "C": [np.nan, 0.5274]},
627+
index=exp_index,
628+
)
629+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)