Skip to content

Commit 1ef8273

Browse files
authored
TST: dt64 units (#56239)
1 parent c1c6db7 commit 1ef8273

File tree

13 files changed

+73
-49
lines changed

13 files changed

+73
-49
lines changed

pandas/core/tools/datetimes.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,15 @@ def _array_strptime_with_fallback(
467467
"""
468468
result, tz_out = array_strptime(arg, fmt, exact=exact, errors=errors, utc=utc)
469469
if tz_out is not None:
470-
dtype = DatetimeTZDtype(tz=tz_out)
470+
unit = np.datetime_data(result.dtype)[0]
471+
dtype = DatetimeTZDtype(tz=tz_out, unit=unit)
471472
dta = DatetimeArray._simple_new(result, dtype=dtype)
472473
if utc:
473474
dta = dta.tz_convert("UTC")
474475
return Index(dta, name=name)
475476
elif result.dtype != object and utc:
476-
res = Index(result, dtype="M8[ns, UTC]", name=name)
477+
unit = np.datetime_data(result.dtype)[0]
478+
res = Index(result, dtype=f"M8[{unit}, UTC]", name=name)
477479
return res
478480
return Index(result, dtype=result.dtype, name=name)
479481

pandas/tests/arithmetic/test_timedelta64.py

-10
Original file line numberDiff line numberDiff line change
@@ -746,20 +746,10 @@ def test_timedelta_ops_with_missing_values(self):
746746
s1 = pd.to_timedelta(Series(["00:00:01"]))
747747
s2 = pd.to_timedelta(Series(["00:00:02"]))
748748

749-
msg = r"dtype datetime64\[ns\] cannot be converted to timedelta64\[ns\]"
750-
with pytest.raises(TypeError, match=msg):
751-
# Passing datetime64-dtype data to TimedeltaIndex is no longer
752-
# supported GH#29794
753-
pd.to_timedelta(Series([NaT])) # TODO: belongs elsewhere?
754-
755749
sn = pd.to_timedelta(Series([NaT], dtype="m8[ns]"))
756750

757751
df1 = DataFrame(["00:00:01"]).apply(pd.to_timedelta)
758752
df2 = DataFrame(["00:00:02"]).apply(pd.to_timedelta)
759-
with pytest.raises(TypeError, match=msg):
760-
# Passing datetime64-dtype data to TimedeltaIndex is no longer
761-
# supported GH#29794
762-
DataFrame([NaT]).apply(pd.to_timedelta) # TODO: belongs elsewhere?
763753

764754
dfn = DataFrame([NaT._value]).apply(pd.to_timedelta)
765755

pandas/tests/arrays/test_array.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def test_array_copy():
268268
),
269269
(
270270
[datetime.datetime(2000, 1, 1), datetime.datetime(2001, 1, 1)],
271-
DatetimeArray._from_sequence(["2000", "2001"]),
271+
DatetimeArray._from_sequence(["2000", "2001"], dtype="M8[ns]"),
272272
),
273273
(
274274
np.array([1, 2], dtype="M8[ns]"),
@@ -284,7 +284,7 @@ def test_array_copy():
284284
(
285285
[pd.Timestamp("2000", tz="CET"), pd.Timestamp("2001", tz="CET")],
286286
DatetimeArray._from_sequence(
287-
["2000", "2001"], dtype=pd.DatetimeTZDtype(tz="CET")
287+
["2000", "2001"], dtype=pd.DatetimeTZDtype(tz="CET", unit="ns")
288288
),
289289
),
290290
(
@@ -293,7 +293,7 @@ def test_array_copy():
293293
datetime.datetime(2001, 1, 1, tzinfo=cet),
294294
],
295295
DatetimeArray._from_sequence(
296-
["2000", "2001"], dtype=pd.DatetimeTZDtype(tz=cet)
296+
["2000", "2001"], dtype=pd.DatetimeTZDtype(tz=cet, unit="ns")
297297
),
298298
),
299299
# timedelta

pandas/tests/frame/constructors/test_from_records.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -442,26 +442,27 @@ def test_from_records_misc_brokenness(self):
442442
exp = DataFrame(data, index=["a", "b", "c"])
443443
tm.assert_frame_equal(result, exp)
444444

445+
def test_from_records_misc_brokenness2(self):
445446
# GH#2623
446447
rows = []
447448
rows.append([datetime(2010, 1, 1), 1])
448449
rows.append([datetime(2010, 1, 2), "hi"]) # test col upconverts to obj
449-
df2_obj = DataFrame.from_records(rows, columns=["date", "test"])
450-
result = df2_obj.dtypes
451-
expected = Series(
452-
[np.dtype("datetime64[ns]"), np.dtype("object")], index=["date", "test"]
450+
result = DataFrame.from_records(rows, columns=["date", "test"])
451+
expected = DataFrame(
452+
{"date": [row[0] for row in rows], "test": [row[1] for row in rows]}
453453
)
454-
tm.assert_series_equal(result, expected)
454+
tm.assert_frame_equal(result, expected)
455+
assert result.dtypes["test"] == np.dtype(object)
455456

457+
def test_from_records_misc_brokenness3(self):
456458
rows = []
457459
rows.append([datetime(2010, 1, 1), 1])
458460
rows.append([datetime(2010, 1, 2), 1])
459-
df2_obj = DataFrame.from_records(rows, columns=["date", "test"])
460-
result = df2_obj.dtypes
461-
expected = Series(
462-
[np.dtype("datetime64[ns]"), np.dtype("int64")], index=["date", "test"]
461+
result = DataFrame.from_records(rows, columns=["date", "test"])
462+
expected = DataFrame(
463+
{"date": [row[0] for row in rows], "test": [row[1] for row in rows]}
463464
)
464-
tm.assert_series_equal(result, expected)
465+
tm.assert_frame_equal(result, expected)
465466

466467
def test_from_records_empty(self):
467468
# GH#3562

pandas/tests/frame/methods/test_replace.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -809,11 +809,13 @@ def test_replace_for_new_dtypes(self, datetime_frame):
809809
Timestamp("20130104", tz="US/Eastern"),
810810
DataFrame(
811811
{
812-
"A": [
813-
Timestamp("20130101", tz="US/Eastern"),
814-
Timestamp("20130104", tz="US/Eastern"),
815-
Timestamp("20130103", tz="US/Eastern"),
816-
],
812+
"A": pd.DatetimeIndex(
813+
[
814+
Timestamp("20130101", tz="US/Eastern"),
815+
Timestamp("20130104", tz="US/Eastern"),
816+
Timestamp("20130103", tz="US/Eastern"),
817+
]
818+
).as_unit("ns"),
817819
"B": [0, np.nan, 2],
818820
}
819821
),
@@ -1174,6 +1176,7 @@ def test_replace_datetimetz(self):
11741176
"B": [0, np.nan, 2],
11751177
}
11761178
)
1179+
expected["A"] = expected["A"].dt.as_unit("ns")
11771180
tm.assert_frame_equal(result, expected)
11781181

11791182
result = df.copy()
@@ -1195,6 +1198,7 @@ def test_replace_datetimetz(self):
11951198
"B": [0, np.nan, 2],
11961199
}
11971200
)
1201+
expected["A"] = expected["A"].dt.as_unit("ns")
11981202
tm.assert_frame_equal(result, expected)
11991203

12001204
result = df.copy()

pandas/tests/frame/methods/test_reset_index.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -699,9 +699,12 @@ def test_reset_index_multiindex_nat():
699699
df = DataFrame({"id": idx, "tstamp": tstamp, "a": list("abc")})
700700
df.loc[2, "tstamp"] = pd.NaT
701701
result = df.set_index(["id", "tstamp"]).reset_index("id")
702+
exp_dti = pd.DatetimeIndex(
703+
["2015-07-01", "2015-07-02", "NaT"], dtype="M8[ns]", name="tstamp"
704+
)
702705
expected = DataFrame(
703706
{"id": range(3), "a": list("abc")},
704-
index=pd.DatetimeIndex(["2015-07-01", "2015-07-02", "NaT"], name="tstamp"),
707+
index=exp_dti,
705708
)
706709
tm.assert_frame_equal(result, expected)
707710

pandas/tests/indexes/datetimes/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -592,13 +592,13 @@ def test_integer_values_and_tz_interpreted_as_utc(self):
592592

593593
result = DatetimeIndex(values).tz_localize("US/Central")
594594

595-
expected = DatetimeIndex(["2000-01-01T00:00:00"], tz="US/Central")
595+
expected = DatetimeIndex(["2000-01-01T00:00:00"], dtype="M8[ns, US/Central]")
596596
tm.assert_index_equal(result, expected)
597597

598598
# but UTC is *not* deprecated.
599599
with tm.assert_produces_warning(None):
600600
result = DatetimeIndex(values, tz="UTC")
601-
expected = DatetimeIndex(["2000-01-01T00:00:00"], tz="UTC")
601+
expected = DatetimeIndex(["2000-01-01T00:00:00"], dtype="M8[ns, UTC]")
602602
tm.assert_index_equal(result, expected)
603603

604604
def test_constructor_coverage(self):

pandas/tests/io/json/test_pandas.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,8 @@ def test_read_timezone_information(self):
16251625
result = read_json(
16261626
StringIO('{"2019-01-01T11:00:00.000Z":88}'), typ="series", orient="index"
16271627
)
1628-
expected = Series([88], index=DatetimeIndex(["2019-01-01 11:00:00"], tz="UTC"))
1628+
exp_dti = DatetimeIndex(["2019-01-01 11:00:00"], dtype="M8[ns, UTC]")
1629+
expected = Series([88], index=exp_dti)
16291630
tm.assert_series_equal(result, expected)
16301631

16311632
@pytest.mark.parametrize(

pandas/tests/reshape/concat/test_datetimes.py

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ def test_concat_datetime_timezone(self):
6161
dtype="M8[ns, Europe/Paris]",
6262
freq="h",
6363
)
64-
6564
expected = DataFrame(
6665
[[1, 1], [2, 2], [3, 3]], index=exp_idx, columns=["a", "b"]
6766
)

pandas/tests/reshape/test_pivot.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,10 @@ def test_pivot_no_values(self):
437437
index=idx,
438438
)
439439
res = df.pivot_table(index=df.index.month, columns=Grouper(key="dt", freq="ME"))
440-
exp_columns = MultiIndex.from_tuples([("A", pd.Timestamp("2011-01-31"))])
441-
exp_columns.names = [None, "dt"]
440+
exp_columns = MultiIndex.from_arrays(
441+
[["A"], pd.DatetimeIndex(["2011-01-31"], dtype="M8[ns]")],
442+
names=[None, "dt"],
443+
)
442444
exp = DataFrame(
443445
[3.25, 2.0], index=Index([1, 2], dtype=np.int32), columns=exp_columns
444446
)

pandas/tests/series/indexing/test_setitem.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ def test_setitem_with_tz(self, tz, indexer_sli):
8888
Timestamp("2016-01-01 00:00", tz=tz),
8989
Timestamp("2011-01-01 00:00", tz=tz),
9090
Timestamp("2016-01-01 02:00", tz=tz),
91-
]
91+
],
92+
dtype=orig.dtype,
9293
)
9394

9495
# scalar
@@ -100,6 +101,7 @@ def test_setitem_with_tz(self, tz, indexer_sli):
100101
vals = Series(
101102
[Timestamp("2011-01-01", tz=tz), Timestamp("2012-01-01", tz=tz)],
102103
index=[1, 2],
104+
dtype=orig.dtype,
103105
)
104106
assert vals.dtype == f"datetime64[ns, {tz}]"
105107

@@ -108,7 +110,8 @@ def test_setitem_with_tz(self, tz, indexer_sli):
108110
Timestamp("2016-01-01 00:00", tz=tz),
109111
Timestamp("2011-01-01 00:00", tz=tz),
110112
Timestamp("2012-01-01 00:00", tz=tz),
111-
]
113+
],
114+
dtype=orig.dtype,
112115
)
113116

114117
ser = orig.copy()

pandas/tests/tools/test_to_datetime.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,11 @@ def test_to_datetime_unit(self, dtype):
20582058
ser = Series([epoch + t for t in range(20)]).astype(dtype)
20592059
result = to_datetime(ser, unit="s")
20602060
expected = Series(
2061-
[Timestamp("2013-06-09 02:42:28") + timedelta(seconds=t) for t in range(20)]
2061+
[
2062+
Timestamp("2013-06-09 02:42:28") + timedelta(seconds=t)
2063+
for t in range(20)
2064+
],
2065+
dtype="M8[ns]",
20622066
)
20632067
tm.assert_series_equal(result, expected)
20642068

@@ -2208,7 +2212,8 @@ def test_dataframe_field_aliases_column_subset(self, df, cache, unit):
22082212
# unit mappings
22092213
result = to_datetime(df[list(unit.keys())].rename(columns=unit), cache=cache)
22102214
expected = Series(
2211-
[Timestamp("20150204 06:58:10"), Timestamp("20160305 07:59:11")]
2215+
[Timestamp("20150204 06:58:10"), Timestamp("20160305 07:59:11")],
2216+
dtype="M8[ns]",
22122217
)
22132218
tm.assert_series_equal(result, expected)
22142219

@@ -2970,7 +2975,8 @@ def test_to_datetime_iso8601_noleading_0s(self, cache, format):
29702975
Timestamp("2015-03-03"),
29712976
]
29722977
)
2973-
tm.assert_series_equal(to_datetime(ser, format=format, cache=cache), expected)
2978+
result = to_datetime(ser, format=format, cache=cache)
2979+
tm.assert_series_equal(result, expected)
29742980

29752981
def test_parse_dates_infer_datetime_format_warning(self):
29762982
# GH 49024
@@ -3364,7 +3370,8 @@ def test_julian(self, julian_dates):
33643370
def test_unix(self):
33653371
result = Series(to_datetime([0, 1, 2], unit="D", origin="unix"))
33663372
expected = Series(
3367-
[Timestamp("1970-01-01"), Timestamp("1970-01-02"), Timestamp("1970-01-03")]
3373+
[Timestamp("1970-01-01"), Timestamp("1970-01-02"), Timestamp("1970-01-03")],
3374+
dtype="M8[ns]",
33683375
)
33693376
tm.assert_series_equal(result, expected)
33703377

@@ -3483,7 +3490,7 @@ def test_arg_tz_ns_unit(self, offset, utc, exp):
34833490
# GH 25546
34843491
arg = "2019-01-01T00:00:00.000" + offset
34853492
result = to_datetime([arg], unit="ns", utc=utc)
3486-
expected = to_datetime([exp])
3493+
expected = to_datetime([exp]).as_unit("ns")
34873494
tm.assert_index_equal(result, expected)
34883495

34893496

@@ -3610,19 +3617,20 @@ def test_to_datetime_monotonic_increasing_index(cache):
36103617
)
36113618
def test_to_datetime_cache_coerce_50_lines_outofbounds(series_length):
36123619
# GH#45319
3613-
s = Series(
3620+
ser = Series(
36143621
[datetime.fromisoformat("1446-04-12 00:00:00+00:00")]
3615-
+ ([datetime.fromisoformat("1991-10-20 00:00:00+00:00")] * series_length)
3622+
+ ([datetime.fromisoformat("1991-10-20 00:00:00+00:00")] * series_length),
3623+
dtype=object,
36163624
)
3617-
result1 = to_datetime(s, errors="coerce", utc=True)
3625+
result1 = to_datetime(ser, errors="coerce", utc=True)
36183626

36193627
expected1 = Series(
36203628
[NaT] + ([Timestamp("1991-10-20 00:00:00+00:00")] * series_length)
36213629
)
36223630

36233631
tm.assert_series_equal(result1, expected1)
36243632

3625-
result2 = to_datetime(s, errors="ignore", utc=True)
3633+
result2 = to_datetime(ser, errors="ignore", utc=True)
36263634

36273635
expected2 = Series(
36283636
[datetime.fromisoformat("1446-04-12 00:00:00+00:00")]
@@ -3632,7 +3640,7 @@ def test_to_datetime_cache_coerce_50_lines_outofbounds(series_length):
36323640
tm.assert_series_equal(result2, expected2)
36333641

36343642
with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond timestamp"):
3635-
to_datetime(s, errors="raise", utc=True)
3643+
to_datetime(ser, errors="raise", utc=True)
36363644

36373645

36383646
def test_to_datetime_format_f_parse_nanos():

pandas/tests/tools/test_to_timedelta.py

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@
2121

2222

2323
class TestTimedeltas:
24+
def test_to_timedelta_dt64_raises(self):
25+
# Passing datetime64-dtype data to TimedeltaIndex is no longer
26+
# supported GH#29794
27+
msg = r"dtype datetime64\[ns\] cannot be converted to timedelta64\[ns\]"
28+
29+
ser = Series([pd.NaT])
30+
with pytest.raises(TypeError, match=msg):
31+
to_timedelta(ser)
32+
with pytest.raises(TypeError, match=msg):
33+
ser.to_frame().apply(to_timedelta)
34+
2435
@pytest.mark.parametrize("readonly", [True, False])
2536
def test_to_timedelta_readonly(self, readonly):
2637
# GH#34857

0 commit comments

Comments
 (0)