Skip to content

Commit 5fdc448

Browse files
committed
update tests for datetime concat
1 parent 9048936 commit 5fdc448

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

pandas/tests/reshape/concat/test_datetimes.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,31 @@ def test_concat_NaT_series_dataframe_all_NaT(self, tz1, tz2):
278278
result = concat([first, second])
279279
tm.assert_frame_equal(result, expected)
280280

281+
def test_concat_ns_and_s_preserves_datetime64(self):
282+
# ensure concatenating a datetime64[ns] column and a copy cast to M8[s]
283+
# yields a datetime64 dtype (finest unit should be ns)
284+
df = pd.DataFrame(
285+
{"ints": range(2), "dates": pd.date_range("2000", periods=2, freq="min")}
286+
)
287+
df2 = df.copy()
288+
df2["dates"] = df2["dates"].astype("M8[s]")
289+
290+
combined = pd.concat([df, df2], ignore_index=True)
291+
292+
# dtype is a datetime64 type
293+
assert pd.api.types.is_datetime64_any_dtype(combined["dates"].dtype)
294+
295+
# unit should be the finest (ns) when mixing ns and s
296+
unit = np.datetime_data(combined["dates"].dtype)[0]
297+
assert unit == "ns"
298+
299+
# values preserved (compare as ns)
300+
exp = pd.to_datetime(list(df["dates"]) + list(df2["dates"]))
301+
tm.assert_series_equal(
302+
combined["dates"].astype("datetime64[ns]").reset_index(drop=True),
303+
pd.Series(exp.astype("datetime64[ns]"), name="dates").reset_index(drop=True),
304+
)
305+
281306

282307
class TestTimezoneConcat:
283308
def test_concat_tz_series(self):
@@ -591,3 +616,25 @@ def test_concat_float_datetime64():
591616

592617
result = concat([df_time, df_float.iloc[:0]])
593618
tm.assert_frame_equal(result, expected)
619+
620+
@pytest.mark.parametrize("order", [[0, 1], [1, 0]])
621+
def test_concat_ns_and_s_order_invariance(order):
622+
df = pd.DataFrame(
623+
{"ints": range(2), "dates": pd.date_range("2000", periods=2, freq="min")}
624+
)
625+
df2 = df.copy()
626+
df2["dates"] = df2["dates"].astype("M8[s]")
627+
628+
parts = [df, df2]
629+
combined = pd.concat([parts[i] for i in order], ignore_index=True)
630+
631+
assert pd.api.types.is_datetime64_any_dtype(combined["dates"].dtype)
632+
633+
634+
def test_concat_ns_and_s_with_all_nat_and_empty():
635+
# mixing a ns datetime column with an all-NaT seconds-typed column
636+
df = pd.DataFrame({"dates": pd.date_range("2000", periods=2, freq="min")})
637+
df2 = pd.DataFrame({"dates": [pd.NaT, pd.NaT]}).astype({"dates": "datetime64[s]"})
638+
639+
combined = pd.concat([df, df2], ignore_index=True)
640+
assert pd.api.types.is_datetime64_any_dtype(combined["dates"].dtype)

0 commit comments

Comments
 (0)