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: specify dt64 units #56217

Merged
merged 2 commits into from
Nov 28, 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
9 changes: 5 additions & 4 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ def test_std_timedelta64_skipna_false(self):
"values", [["2022-01-01", "2022-01-02", pd.NaT, "2022-01-03"], 4 * [pd.NaT]]
)
def test_std_datetime64_with_nat(
self, values, skipna, using_array_manager, request
self, values, skipna, using_array_manager, request, unit
):
# GH#51335
if using_array_manager and (
Expand All @@ -808,13 +808,14 @@ def test_std_datetime64_with_nat(
reason="GH#51446: Incorrect type inference on NaT in reduction result"
)
request.applymarker(mark)
df = DataFrame({"a": to_datetime(values)})
dti = to_datetime(values).as_unit(unit)
df = DataFrame({"a": dti})
result = df.std(skipna=skipna)
if not skipna or all(value is pd.NaT for value in values):
expected = Series({"a": pd.NaT}, dtype="timedelta64[ns]")
expected = Series({"a": pd.NaT}, dtype=f"timedelta64[{unit}]")
else:
# 86400000000000ns == 1 day
expected = Series({"a": 86400000000000}, dtype="timedelta64[ns]")
expected = Series({"a": 86400000000000}, dtype=f"timedelta64[{unit}]")
tm.assert_series_equal(result, expected)

def test_sum_corner(self):
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/groupby/test_timegrouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,9 @@ def test_groupby_groups_datetimeindex2(self):
for date in dates:
result = grouped.get_group(date)
data = [[df.loc[date, "A"], df.loc[date, "B"]]]
expected_index = DatetimeIndex([date], name="date", freq="D")
expected_index = DatetimeIndex(
[date], name="date", freq="D", dtype=index.dtype
)
expected = DataFrame(data, columns=list("AB"), index=expected_index)
tm.assert_frame_equal(result, expected)

Expand Down
31 changes: 17 additions & 14 deletions pandas/tests/indexes/datetimes/methods/test_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def test_round_invalid(self, freq, error_msg):
with pytest.raises(ValueError, match=error_msg):
dti.round(freq)

def test_round(self, tz_naive_fixture):
def test_round(self, tz_naive_fixture, unit):
tz = tz_naive_fixture
rng = date_range(start="2016-01-01", periods=5, freq="30Min", tz=tz)
rng = date_range(start="2016-01-01", periods=5, freq="30Min", tz=tz, unit=unit)
elt = rng[1]

expected_rng = DatetimeIndex(
Expand All @@ -53,10 +53,11 @@ def test_round(self, tz_naive_fixture):
Timestamp("2016-01-01 02:00:00", tz=tz),
Timestamp("2016-01-01 02:00:00", tz=tz),
]
)
).as_unit(unit)
expected_elt = expected_rng[1]

tm.assert_index_equal(rng.round(freq="h"), expected_rng)
result = rng.round(freq="h")
tm.assert_index_equal(result, expected_rng)
assert elt.round(freq="h") == expected_elt

msg = INVALID_FREQ_ERR_MSG
Expand All @@ -74,30 +75,31 @@ def test_round(self, tz_naive_fixture):
def test_round2(self, tz_naive_fixture):
tz = tz_naive_fixture
# GH#14440 & GH#15578
index = DatetimeIndex(["2016-10-17 12:00:00.0015"], tz=tz)
index = DatetimeIndex(["2016-10-17 12:00:00.0015"], tz=tz).as_unit("ns")
result = index.round("ms")
expected = DatetimeIndex(["2016-10-17 12:00:00.002000"], tz=tz)
expected = DatetimeIndex(["2016-10-17 12:00:00.002000"], tz=tz).as_unit("ns")
tm.assert_index_equal(result, expected)

for freq in ["us", "ns"]:
tm.assert_index_equal(index, index.round(freq))

def test_round3(self, tz_naive_fixture):
tz = tz_naive_fixture
index = DatetimeIndex(["2016-10-17 12:00:00.00149"], tz=tz)
index = DatetimeIndex(["2016-10-17 12:00:00.00149"], tz=tz).as_unit("ns")
result = index.round("ms")
expected = DatetimeIndex(["2016-10-17 12:00:00.001000"], tz=tz)
expected = DatetimeIndex(["2016-10-17 12:00:00.001000"], tz=tz).as_unit("ns")
tm.assert_index_equal(result, expected)

def test_round4(self, tz_naive_fixture):
index = DatetimeIndex(["2016-10-17 12:00:00.001501031"])
index = DatetimeIndex(["2016-10-17 12:00:00.001501031"], dtype="M8[ns]")
result = index.round("10ns")
expected = DatetimeIndex(["2016-10-17 12:00:00.001501030"])
expected = DatetimeIndex(["2016-10-17 12:00:00.001501030"], dtype="M8[ns]")
tm.assert_index_equal(result, expected)

ts = "2016-10-17 12:00:00.001501031"
dti = DatetimeIndex([ts], dtype="M8[ns]")
with tm.assert_produces_warning(False):
ts = "2016-10-17 12:00:00.001501031"
DatetimeIndex([ts]).round("1010ns")
dti.round("1010ns")

def test_no_rounding_occurs(self, tz_naive_fixture):
# GH 21262
Expand All @@ -112,9 +114,10 @@ def test_no_rounding_occurs(self, tz_naive_fixture):
Timestamp("2016-01-01 00:06:00", tz=tz),
Timestamp("2016-01-01 00:08:00", tz=tz),
]
)
).as_unit("ns")

tm.assert_index_equal(rng.round(freq="2min"), expected_rng)
result = rng.round(freq="2min")
tm.assert_index_equal(result, expected_rng)

@pytest.mark.parametrize(
"test_input, rounder, freq, expected",
Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/indexes/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,14 @@ def test_constructor_dtype(self):
idx = DatetimeIndex(
["2013-01-01", "2013-01-02"], dtype="datetime64[ns, US/Eastern]"
)
expected = DatetimeIndex(["2013-01-01", "2013-01-02"]).tz_localize("US/Eastern")
expected = (
DatetimeIndex(["2013-01-01", "2013-01-02"])
.as_unit("ns")
.tz_localize("US/Eastern")
)
tm.assert_index_equal(idx, expected)

idx = DatetimeIndex(["2013-01-01", "2013-01-02"], tz="US/Eastern")
idx = DatetimeIndex(["2013-01-01", "2013-01-02"], tz="US/Eastern").as_unit("ns")
tm.assert_index_equal(idx, expected)

def test_constructor_dtype_tz_mismatch_raises(self):
Expand Down Expand Up @@ -774,7 +778,7 @@ def test_constructor_start_end_with_tz(self, tz):
result = date_range(freq="D", start=start, end=end, tz=tz)
expected = DatetimeIndex(
["2013-01-01 06:00:00", "2013-01-02 06:00:00"],
tz="America/Los_Angeles",
dtype="M8[ns, America/Los_Angeles]",
freq="D",
)
tm.assert_index_equal(result, expected)
Expand Down
88 changes: 42 additions & 46 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,58 +226,54 @@ def test_take_nan_first_datetime(self):
expected = DatetimeIndex([index[-1], index[0], index[1]])
tm.assert_index_equal(result, expected)

def test_take(self):
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo"])
def test_take(self, tz):
# GH#10295
idx1 = date_range("2011-01-01", "2011-01-31", freq="D", name="idx")
idx2 = date_range(
"2011-01-01", "2011-01-31", freq="D", tz="Asia/Tokyo", name="idx"
idx = date_range("2011-01-01", "2011-01-31", freq="D", name="idx", tz=tz)

result = idx.take([0])
assert result == Timestamp("2011-01-01", tz=idx.tz)

result = idx.take([0, 1, 2])
expected = date_range(
"2011-01-01", "2011-01-03", freq="D", tz=idx.tz, name="idx"
)
tm.assert_index_equal(result, expected)
assert result.freq == expected.freq

for idx in [idx1, idx2]:
result = idx.take([0])
assert result == Timestamp("2011-01-01", tz=idx.tz)
result = idx.take([0, 2, 4])
expected = date_range(
"2011-01-01", "2011-01-05", freq="2D", tz=idx.tz, name="idx"
)
tm.assert_index_equal(result, expected)
assert result.freq == expected.freq

result = idx.take([0, 1, 2])
expected = date_range(
"2011-01-01", "2011-01-03", freq="D", tz=idx.tz, name="idx"
)
tm.assert_index_equal(result, expected)
assert result.freq == expected.freq
result = idx.take([7, 4, 1])
expected = date_range(
"2011-01-08", "2011-01-02", freq="-3D", tz=idx.tz, name="idx"
)
tm.assert_index_equal(result, expected)
assert result.freq == expected.freq

result = idx.take([0, 2, 4])
expected = date_range(
"2011-01-01", "2011-01-05", freq="2D", tz=idx.tz, name="idx"
)
tm.assert_index_equal(result, expected)
assert result.freq == expected.freq
result = idx.take([3, 2, 5])
expected = DatetimeIndex(
["2011-01-04", "2011-01-03", "2011-01-06"],
dtype=idx.dtype,
freq=None,
name="idx",
)
tm.assert_index_equal(result, expected)
assert result.freq is None

result = idx.take([7, 4, 1])
expected = date_range(
"2011-01-08", "2011-01-02", freq="-3D", tz=idx.tz, name="idx"
)
tm.assert_index_equal(result, expected)
assert result.freq == expected.freq

result = idx.take([3, 2, 5])
expected = DatetimeIndex(
["2011-01-04", "2011-01-03", "2011-01-06"],
dtype=idx.dtype,
freq=None,
name="idx",
)
tm.assert_index_equal(result, expected)
assert result.freq is None

result = idx.take([-3, 2, 5])
expected = DatetimeIndex(
["2011-01-29", "2011-01-03", "2011-01-06"],
dtype=idx.dtype,
freq=None,
tz=idx.tz,
name="idx",
)
tm.assert_index_equal(result, expected)
assert result.freq is None
result = idx.take([-3, 2, 5])
expected = DatetimeIndex(
["2011-01-29", "2011-01-03", "2011-01-06"],
dtype=idx.dtype,
freq=None,
name="idx",
)
tm.assert_index_equal(result, expected)
assert result.freq is None

def test_take_invalid_kwargs(self):
idx = date_range("2011-01-01", "2011-01-31", freq="D", name="idx")
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexing/test_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ def test_insert_float_index(
def test_insert_index_datetimes(self, fill_val, exp_dtype, insert_value):
obj = pd.DatetimeIndex(
["2011-01-01", "2011-01-02", "2011-01-03", "2011-01-04"], tz=fill_val.tz
)
).as_unit("ns")
assert obj.dtype == exp_dtype

exp = pd.DatetimeIndex(
["2011-01-01", fill_val.date(), "2011-01-02", "2011-01-03", "2011-01-04"],
tz=fill_val.tz,
)
).as_unit("ns")
self._assert_insert_conversion(obj, fill_val, exp, exp_dtype)

if fill_val.tz:
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ def test_indexing_fast_xs(self):
df = DataFrame({"a": date_range("2014-01-01", periods=10, tz="UTC")})
result = df.iloc[5]
expected = Series(
[Timestamp("2014-01-06 00:00:00+0000", tz="UTC")], index=["a"], name=5
[Timestamp("2014-01-06 00:00:00+0000", tz="UTC")],
index=["a"],
name=5,
dtype="M8[ns, UTC]",
)
tm.assert_series_equal(result, expected)

Expand Down
38 changes: 19 additions & 19 deletions pandas/tests/reshape/concat/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,14 @@ def test_concat_datetime_timezone(self):
df2 = DataFrame({"b": [1, 2, 3]}, index=idx2)
result = concat([df1, df2], axis=1)

exp_idx = (
DatetimeIndex(
[
"2011-01-01 00:00:00+01:00",
"2011-01-01 01:00:00+01:00",
"2011-01-01 02:00:00+01:00",
],
freq="h",
)
.tz_convert("UTC")
.tz_convert("Europe/Paris")
.as_unit("ns")
exp_idx = DatetimeIndex(
[
"2011-01-01 00:00:00+01:00",
"2011-01-01 01:00:00+01:00",
"2011-01-01 02:00:00+01:00",
],
dtype="M8[ns, Europe/Paris]",
freq="h",
)

expected = DataFrame(
Expand Down Expand Up @@ -431,21 +427,25 @@ def test_concat_multiindex_with_tz(self):
# GH 6606
df = DataFrame(
{
"dt": [
datetime(2014, 1, 1),
datetime(2014, 1, 2),
datetime(2014, 1, 3),
],
"dt": DatetimeIndex(
[
datetime(2014, 1, 1),
datetime(2014, 1, 2),
datetime(2014, 1, 3),
],
dtype="M8[ns, US/Pacific]",
),
"b": ["A", "B", "C"],
"c": [1, 2, 3],
"d": [4, 5, 6],
}
)
df["dt"] = df["dt"].apply(lambda d: Timestamp(d, tz="US/Pacific"))
df = df.set_index(["dt", "b"])

exp_idx1 = DatetimeIndex(
["2014-01-01", "2014-01-02", "2014-01-03"] * 2, tz="US/Pacific", name="dt"
["2014-01-01", "2014-01-02", "2014-01-03"] * 2,
dtype="M8[ns, US/Pacific]",
name="dt",
)
exp_idx2 = Index(["A", "B", "C"] * 2, name="b")
exp_idx = MultiIndex.from_arrays([exp_idx1, exp_idx2])
Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ def test_setitem_with_tz_dst(self, indexer_sli):
Timestamp("2016-11-06 00:00-04:00", tz=tz),
Timestamp("2011-01-01 00:00-05:00", tz=tz),
Timestamp("2016-11-06 01:00-05:00", tz=tz),
]
],
dtype=orig.dtype,
)

# scalar
Expand All @@ -138,6 +139,7 @@ def test_setitem_with_tz_dst(self, indexer_sli):
vals = Series(
[Timestamp("2011-01-01", tz=tz), Timestamp("2012-01-01", tz=tz)],
index=[1, 2],
dtype=orig.dtype,
)
assert vals.dtype == f"datetime64[ns, {tz}]"

Expand All @@ -146,7 +148,8 @@ def test_setitem_with_tz_dst(self, indexer_sli):
Timestamp("2016-11-06 00:00", tz=tz),
Timestamp("2011-01-01 00:00", tz=tz),
Timestamp("2012-01-01 00:00", tz=tz),
]
],
dtype=orig.dtype,
)

ser = orig.copy()
Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,24 +1154,24 @@ def test_constructor_with_datetime_tz5(self):

def test_constructor_with_datetime_tz4(self):
# inference
s = Series(
ser = Series(
[
Timestamp("2013-01-01 13:00:00-0800", tz="US/Pacific"),
Timestamp("2013-01-02 14:00:00-0800", tz="US/Pacific"),
]
)
assert s.dtype == "datetime64[ns, US/Pacific]"
assert lib.infer_dtype(s, skipna=True) == "datetime64"
assert ser.dtype == "datetime64[ns, US/Pacific]"
assert lib.infer_dtype(ser, skipna=True) == "datetime64"

def test_constructor_with_datetime_tz3(self):
s = Series(
ser = Series(
[
Timestamp("2013-01-01 13:00:00-0800", tz="US/Pacific"),
Timestamp("2013-01-02 14:00:00-0800", tz="US/Eastern"),
]
)
assert s.dtype == "object"
assert lib.infer_dtype(s, skipna=True) == "datetime"
assert ser.dtype == "object"
assert lib.infer_dtype(ser, skipna=True) == "datetime"

def test_constructor_with_datetime_tz2(self):
# with all NaT
Expand Down Expand Up @@ -1587,7 +1587,7 @@ def test_NaT_scalar(self):
def test_NaT_cast(self):
# GH10747
result = Series([np.nan]).astype("M8[ns]")
expected = Series([NaT])
expected = Series([NaT], dtype="M8[ns]")
tm.assert_series_equal(result, expected)

def test_constructor_name_hashable(self):
Expand Down
Loading
Loading