Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: parametrize over dt64 unit #56052

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2244,13 +2244,11 @@ def _sequence_to_dt64(
if tz and inferred_tz:
# two timezones: convert to intended from base UTC repr
# GH#42505 by convention, these are _already_ UTC
assert converted.dtype == out_dtype, converted.dtype
result = converted.view(out_dtype)
result = converted

elif inferred_tz:
tz = inferred_tz
assert converted.dtype == out_dtype, converted.dtype
result = converted.view(out_dtype)
result = converted

else:
result, _ = _construct_from_dt64_naive(
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ def test_nuiscance_columns():

result = df.agg(["min"])
expected = DataFrame(
[[1, 1.0, "bar", Timestamp("20130101")]],
[[1, 1.0, "bar", Timestamp("20130101").as_unit("ns")]],
index=["min"],
columns=df.columns,
)
Expand Down
32 changes: 19 additions & 13 deletions pandas/tests/apply/test_series_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,39 +150,45 @@ def func(x):
tm.assert_series_equal(result, expected)


def test_apply_box():
def test_apply_box_dt64():
# ufunc will not be boxed. Same test cases as the test_map_box
vals = [pd.Timestamp("2011-01-01"), pd.Timestamp("2011-01-02")]
s = Series(vals)
assert s.dtype == "datetime64[ns]"
ser = Series(vals, dtype="M8[ns]")
assert ser.dtype == "datetime64[ns]"
# boxed value must be Timestamp instance
res = s.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}", by_row="compat")
res = ser.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}", by_row="compat")
exp = Series(["Timestamp_1_None", "Timestamp_2_None"])
tm.assert_series_equal(res, exp)


def test_apply_box_dt64tz():
vals = [
pd.Timestamp("2011-01-01", tz="US/Eastern"),
pd.Timestamp("2011-01-02", tz="US/Eastern"),
]
s = Series(vals)
assert s.dtype == "datetime64[ns, US/Eastern]"
res = s.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}", by_row="compat")
ser = Series(vals, dtype="M8[ns, US/Eastern]")
assert ser.dtype == "datetime64[ns, US/Eastern]"
res = ser.apply(lambda x: f"{type(x).__name__}_{x.day}_{x.tz}", by_row="compat")
exp = Series(["Timestamp_1_US/Eastern", "Timestamp_2_US/Eastern"])
tm.assert_series_equal(res, exp)


def test_apply_box_td64():
# timedelta
vals = [pd.Timedelta("1 days"), pd.Timedelta("2 days")]
s = Series(vals)
assert s.dtype == "timedelta64[ns]"
res = s.apply(lambda x: f"{type(x).__name__}_{x.days}", by_row="compat")
ser = Series(vals)
assert ser.dtype == "timedelta64[ns]"
res = ser.apply(lambda x: f"{type(x).__name__}_{x.days}", by_row="compat")
exp = Series(["Timedelta_1", "Timedelta_2"])
tm.assert_series_equal(res, exp)


def test_apply_box_period():
# period
vals = [pd.Period("2011-01-01", freq="M"), pd.Period("2011-01-02", freq="M")]
s = Series(vals)
assert s.dtype == "Period[M]"
res = s.apply(lambda x: f"{type(x).__name__}_{x.freqstr}", by_row="compat")
ser = Series(vals)
assert ser.dtype == "Period[M]"
res = ser.apply(lambda x: f"{type(x).__name__}_{x.freqstr}", by_row="compat")
exp = Series(["Period_M", "Period_M"])
tm.assert_series_equal(res, exp)

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ def test_dti_add_tick_tzaware(self, tz_aware_fixture, box_with_array):
["2010-11-01 05:00", "2010-11-01 06:00", "2010-11-01 07:00"],
freq="h",
tz=tz,
)
).as_unit("ns")

dates = tm.box_expected(dates, box_with_array)
expected = tm.box_expected(expected, box_with_array)
Expand Down Expand Up @@ -1580,7 +1580,7 @@ def test_dti_add_sub_nonzero_mth_offset(
mth = getattr(date, op)
result = mth(offset)

expected = DatetimeIndex(exp, tz=tz)
expected = DatetimeIndex(exp, tz=tz).as_unit("ns")
expected = tm.box_expected(expected, box_with_array, False)
tm.assert_equal(result, expected)

Expand Down Expand Up @@ -2286,7 +2286,7 @@ def test_dti_add_series(self, tz_naive_fixture, names):
tz = tz_naive_fixture
index = DatetimeIndex(
["2016-06-28 05:30", "2016-06-28 05:31"], tz=tz, name=names[0]
)
).as_unit("ns")
ser = Series([Timedelta(seconds=5)] * 2, index=index, name=names[1])
expected = Series(index + Timedelta(seconds=5), index=index, name=names[2])

Expand Down
26 changes: 17 additions & 9 deletions pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,14 @@ def test_subtraction_ops(self):

result = dti - td
expected = DatetimeIndex(
["20121231", "20130101", "20130102"], freq="D", name="bar"
["20121231", "20130101", "20130102"], dtype="M8[ns]", freq="D", name="bar"
)
tm.assert_index_equal(result, expected)

result = dt - tdi
expected = DatetimeIndex(["20121231", NaT, "20121230"], name="foo")
expected = DatetimeIndex(
["20121231", NaT, "20121230"], dtype="M8[ns]", name="foo"
)
tm.assert_index_equal(result, expected)

def test_subtraction_ops_with_tz(self, box_with_array):
Expand Down Expand Up @@ -432,7 +434,9 @@ def _check(result, expected):
_check(result, expected)

result = dti_tz - td
expected = DatetimeIndex(["20121231", "20130101", "20130102"], tz="US/Eastern")
expected = DatetimeIndex(
["20121231", "20130101", "20130102"], tz="US/Eastern"
).as_unit("ns")
expected = tm.box_expected(expected, box_with_array)
tm.assert_equal(result, expected)

Expand All @@ -450,7 +454,7 @@ def test_dti_tdi_numeric_ops(self):
tm.assert_index_equal(result, expected)

result = dti - tdi # name will be reset
expected = DatetimeIndex(["20121231", NaT, "20130101"])
expected = DatetimeIndex(["20121231", NaT, "20130101"], dtype="M8[ns]")
tm.assert_index_equal(result, expected)

def test_addition_ops(self):
Expand All @@ -461,11 +465,15 @@ def test_addition_ops(self):
dt = Timestamp("20130101")

result = tdi + dt
expected = DatetimeIndex(["20130102", NaT, "20130103"], name="foo")
expected = DatetimeIndex(
["20130102", NaT, "20130103"], dtype="M8[ns]", name="foo"
)
tm.assert_index_equal(result, expected)

result = dt + tdi
expected = DatetimeIndex(["20130102", NaT, "20130103"], name="foo")
expected = DatetimeIndex(
["20130102", NaT, "20130103"], dtype="M8[ns]", name="foo"
)
tm.assert_index_equal(result, expected)

result = td + tdi
Expand All @@ -492,11 +500,11 @@ def test_addition_ops(self):
# pytest.raises(TypeError, lambda : Index([1,2,3]) + tdi)

result = tdi + dti # name will be reset
expected = DatetimeIndex(["20130102", NaT, "20130105"])
expected = DatetimeIndex(["20130102", NaT, "20130105"], dtype="M8[ns]")
tm.assert_index_equal(result, expected)

result = dti + tdi # name will be reset
expected = DatetimeIndex(["20130102", NaT, "20130105"])
expected = DatetimeIndex(["20130102", NaT, "20130105"], dtype="M8[ns]")
tm.assert_index_equal(result, expected)

result = dt + td
Expand Down Expand Up @@ -869,7 +877,7 @@ def test_operators_timedelta64(self):
# timestamp on lhs
result = resultb + df["A"]
values = [Timestamp("20111230"), Timestamp("20120101"), Timestamp("20120103")]
expected = Series(values, name="A")
expected = Series(values, dtype="M8[ns]", name="A")
tm.assert_series_equal(result, expected)

# datetimes on rhs
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/period/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ def test_astype_datetime(dtype):
else:
# GH#45038 allow period->dt64 because we allow dt64->period
result = arr.astype(dtype)
expected = pd.DatetimeIndex(["2000", "2001", pd.NaT])._data
expected = pd.DatetimeIndex(["2000", "2001", pd.NaT], dtype=dtype)._data
tm.assert_datetime_array_equal(result, expected)
6 changes: 3 additions & 3 deletions pandas/tests/arrays/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ def test_dt64_array(dtype_unit):
(
pd.DatetimeIndex(["2000", "2001"]),
np.dtype("datetime64[ns]"),
DatetimeArray._from_sequence(["2000", "2001"]),
DatetimeArray._from_sequence(["2000", "2001"], dtype="M8[ns]"),
),
(
pd.DatetimeIndex(["2000", "2001"]),
None,
DatetimeArray._from_sequence(["2000", "2001"]),
DatetimeArray._from_sequence(["2000", "2001"], dtype="M8[ns]"),
),
(
["2000", "2001"],
np.dtype("datetime64[ns]"),
DatetimeArray._from_sequence(["2000", "2001"]),
DatetimeArray._from_sequence(["2000", "2001"], dtype="M8[ns]"),
),
# Datetime (tz-aware)
(
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/frame/methods/test_asfreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_asfreq2(self, frame_or_series):
datetime(2009, 11, 30),
datetime(2009, 12, 31),
],
dtype="M8[ns]",
freq="BME",
),
)
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,7 @@ def test_frame_with_frame_reindex(self):
"bar": [pd.Timestamp("2018"), pd.Timestamp("2021")],
},
columns=["foo", "bar"],
dtype="M8[ns]",
)
df2 = df[["foo"]]

Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/indexes/datetimes/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def test_dti_astype_asobject_around_dst_transition(self, tzstr):

def test_astype(self):
# GH 13149, GH 13209
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.nan], name="idx")
idx = DatetimeIndex(
["2016-05-16", "NaT", NaT, np.nan], dtype="M8[ns]", name="idx"
)

result = idx.astype(object)
expected = Index(
Expand All @@ -55,6 +57,7 @@ def test_astype(self):
)
tm.assert_index_equal(result, expected)

def test_astype2(self):
rng = date_range("1/1/2000", periods=10, name="idx")
result = rng.astype("i8")
tm.assert_index_equal(result, Index(rng.asi8, name="idx"))
Expand Down Expand Up @@ -160,7 +163,9 @@ def test_astype_str_freq_and_tz(self):

def test_astype_datetime64(self):
# GH 13149, GH 13209
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.nan], name="idx")
idx = DatetimeIndex(
["2016-05-16", "NaT", NaT, np.nan], dtype="M8[ns]", name="idx"
)

result = idx.astype("datetime64[ns]")
tm.assert_index_equal(result, idx)
Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/indexes/datetimes/methods/test_tz_localize.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,7 @@ def test_dti_tz_localize_ambiguous_flags(self, tz, unit):
tm.assert_index_equal(expected, localized)

result = DatetimeIndex(times, tz=tz, ambiguous=is_dst).as_unit(unit)
tm.assert_index_equal(
result,
expected,
)
tm.assert_index_equal(result, expected)

localized = di.tz_localize(tz, ambiguous=np.array(is_dst))
tm.assert_index_equal(dr, localized)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/datetimes/methods/test_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def test_index_unique(rand_series_with_duplicate_datetimeindex):
datetime(2000, 1, 4),
datetime(2000, 1, 5),
],
dtype="M8[ns]",
dtype=index.dtype,
)
assert uniques.dtype == "M8[ns]" # sanity
assert uniques.dtype == index.dtype # sanity
tm.assert_index_equal(uniques, expected)
assert index.nunique() == 4

Expand Down
18 changes: 10 additions & 8 deletions pandas/tests/resample/test_period_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,10 @@ def test_resample_with_dst_time_change(self):
"2016-03-15 01:00:00-05:00",
"2016-03-15 13:00:00-05:00",
]
index = pd.to_datetime(expected_index_values, utc=True).tz_convert(
"America/Chicago"
index = (
pd.to_datetime(expected_index_values, utc=True)
.tz_convert("America/Chicago")
.as_unit(index.unit)
)
index = pd.DatetimeIndex(index, freq="12h")
expected = DataFrame(
Expand Down Expand Up @@ -668,15 +670,15 @@ def test_default_left_closed_label(self, from_freq, to_freq):
)

def test_all_values_single_bin(self):
# 2070
# GH#2070
index = period_range(start="2012-01-01", end="2012-12-31", freq="M")
s = Series(np.random.default_rng(2).standard_normal(len(index)), index=index)
ser = Series(np.random.default_rng(2).standard_normal(len(index)), index=index)

result = s.resample("Y").mean()
tm.assert_almost_equal(result.iloc[0], s.mean())
result = ser.resample("Y").mean()
tm.assert_almost_equal(result.iloc[0], ser.mean())

def test_evenly_divisible_with_no_extra_bins(self):
# 4076
# GH#4076
# when the frequency is evenly divisible, sometimes extra bins

df = DataFrame(
Expand All @@ -686,7 +688,7 @@ def test_evenly_divisible_with_no_extra_bins(self):
result = df.resample("5D").mean()
expected = pd.concat([df.iloc[0:5].mean(), df.iloc[5:].mean()], axis=1).T
expected.index = pd.DatetimeIndex(
[Timestamp("2000-1-1"), Timestamp("2000-1-6")], freq="5D"
[Timestamp("2000-1-1"), Timestamp("2000-1-6")], dtype="M8[ns]", freq="5D"
)
tm.assert_frame_equal(result, expected)

Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ def test_resample_group_keys():

# group_keys=True
expected.index = pd.MultiIndex.from_arrays(
[pd.to_datetime(["2000-01-01", "2000-01-06"]).repeat(5), expected.index]
[
pd.to_datetime(["2000-01-01", "2000-01-06"]).as_unit("ns").repeat(5),
expected.index,
]
)
g = df.resample("5D", group_keys=True)
result = g.apply(lambda x: x)
Expand Down
24 changes: 17 additions & 7 deletions pandas/tests/resample/test_time_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ def test_aggregate_nth():
],
)
def test_resample_entirely_nat_window(method, method_args, unit):
s = Series([0] * 2 + [np.nan] * 2, index=date_range("2017", periods=4))
result = methodcaller(method, **method_args)(s.resample("2d"))
expected = Series(
[0.0, unit], index=pd.DatetimeIndex(["2017-01-01", "2017-01-03"], freq="2D")
)
ser = Series([0] * 2 + [np.nan] * 2, index=date_range("2017", periods=4))
result = methodcaller(method, **method_args)(ser.resample("2d"))

exp_dti = pd.DatetimeIndex(["2017-01-01", "2017-01-03"], dtype="M8[ns]", freq="2D")
expected = Series([0.0, unit], index=exp_dti)
tm.assert_series_equal(result, expected)


Expand Down Expand Up @@ -233,7 +233,13 @@ def test_aggregate_with_nat(func, fill_value):
pad = DataFrame([[fill_value] * 4], index=[3], columns=["A", "B", "C", "D"])
expected = pd.concat([normal_result, pad])
expected = expected.sort_index()
dti = date_range(start="2013-01-01", freq="D", periods=5, name="key")
dti = date_range(
start="2013-01-01",
freq="D",
periods=5,
name="key",
unit=dt_df["key"]._values.unit,
)
expected.index = dti._with_freq(None) # TODO: is this desired?
tm.assert_frame_equal(expected, dt_result)
assert dt_result.index.name == "key"
Expand Down Expand Up @@ -265,7 +271,11 @@ def test_aggregate_with_nat_size():
expected = pd.concat([normal_result, pad])
expected = expected.sort_index()
expected.index = date_range(
start="2013-01-01", freq="D", periods=5, name="key"
start="2013-01-01",
freq="D",
periods=5,
name="key",
unit=dt_df["key"]._values.unit,
)._with_freq(None)
tm.assert_series_equal(expected, dt_result)
assert dt_result.index.name == "key"
Expand Down
Loading
Loading