Skip to content

Commit

Permalink
TST: dt64 unit in tests (#56126)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Nov 23, 2023
1 parent 68ef1da commit 1c606d5
Show file tree
Hide file tree
Showing 24 changed files with 182 additions and 226 deletions.
3 changes: 2 additions & 1 deletion pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ def _convert_listlike_datetimes(
if tz_parsed is not None:
# We can take a shortcut since the datetime64 numpy array
# is in UTC
dtype = cast(DatetimeTZDtype, tz_to_dtype(tz_parsed))
out_unit = np.datetime_data(result.dtype)[0]
dtype = cast(DatetimeTZDtype, tz_to_dtype(tz_parsed, out_unit))
dt64_values = result.view(f"M8[{dtype.unit}]")
dta = DatetimeArray._simple_new(dt64_values, dtype=dtype)
return DatetimeIndex._simple_new(dta, name=name)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ def test_td64arr_add_datetime64_nat(self, box_with_array):
other = np.datetime64("NaT")

tdi = timedelta_range("1 day", periods=3)
expected = DatetimeIndex(["NaT", "NaT", "NaT"])
expected = DatetimeIndex(["NaT", "NaT", "NaT"], dtype="M8[ns]")

tdser = tm.box_expected(tdi, box_with_array)
expected = tm.box_expected(expected, box_with_array)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ def test_is_boolean(self, categories, expected):

def test_dtype_specific_categorical_dtype(self):
expected = "datetime64[ns]"
result = str(Categorical(DatetimeIndex([])).categories.dtype)
dti = DatetimeIndex([], dtype=expected)
result = str(Categorical(dti).categories.dtype)
assert result == expected

def test_not_string(self):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/methods/test_transpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def test_transpose_td64_intervals(self):

def test_transpose_empty_preserves_datetimeindex(self):
# GH#41382
df = DataFrame(index=DatetimeIndex([]))
dti = DatetimeIndex([], dtype="M8[ns]")
df = DataFrame(index=dti)

expected = DatetimeIndex([], dtype="datetime64[ns]", freq=None)

Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def test_date_range_edges(self, freq):
)
exp = DatetimeIndex(
[ts + n * td for n in range(1, 5)],
dtype="M8[ns]",
freq=freq,
)
tm.assert_index_equal(idx, exp)
Expand All @@ -193,7 +194,7 @@ def test_date_range_edges(self, freq):
end=ts + td,
freq=freq,
)
exp = DatetimeIndex([], freq=freq)
exp = DatetimeIndex([], dtype="M8[ns]", freq=freq)
tm.assert_index_equal(idx, exp)

# start matches end
Expand All @@ -202,7 +203,7 @@ def test_date_range_edges(self, freq):
end=ts + td,
freq=freq,
)
exp = DatetimeIndex([ts + td], freq=freq)
exp = DatetimeIndex([ts + td], dtype="M8[ns]", freq=freq)
tm.assert_index_equal(idx, exp)

def test_date_range_near_implementation_bound(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/datetimes/test_scalar_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def test_dti_fields(self, tz):
exp = dti[[0, 90, 181, 273]]
tm.assert_index_equal(res, exp)
res = dti[dti.is_leap_year]
exp = DatetimeIndex([], freq="D", tz=dti.tz, name="name")
exp = DatetimeIndex([], freq="D", tz=dti.tz, name="name").as_unit("ns")
tm.assert_index_equal(res, exp)

def test_dti_is_year_quarter_start(self):
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_union(self, tz, sort):
expected2_notsorted = DatetimeIndex(list(other2) + list(rng2[:3]))

rng3 = date_range("1/1/2000", freq="D", periods=5, tz=tz)
other3 = DatetimeIndex([], tz=tz)
other3 = DatetimeIndex([], tz=tz).as_unit("ns")
expected3 = date_range("1/1/2000", freq="D", periods=5, tz=tz)
expected3_notsorted = rng3

Expand Down Expand Up @@ -235,7 +235,7 @@ def test_intersection(self, tz, sort):
expected3 = date_range("6/1/2000", "6/20/2000", freq="D", name=None)

rng4 = date_range("7/1/2000", "7/31/2000", freq="D", name="idx")
expected4 = DatetimeIndex([], freq="D", name="idx")
expected4 = DatetimeIndex([], freq="D", name="idx", dtype="M8[ns]")

for rng, expected in [
(rng2, expected2),
Expand Down Expand Up @@ -265,7 +265,7 @@ def test_intersection(self, tz, sort):

# GH 7880
rng4 = date_range("7/1/2000", "7/31/2000", freq="D", tz=tz, name="idx")
expected4 = DatetimeIndex([], tz=tz, name="idx")
expected4 = DatetimeIndex([], tz=tz, name="idx").as_unit("ns")
assert expected4.freq is None

for rng, expected in [
Expand Down Expand Up @@ -536,7 +536,7 @@ def test_intersection(self):

# non-overlapping
the_int = rng[:10].intersection(rng[10:])
expected = DatetimeIndex([])
expected = DatetimeIndex([]).as_unit("ns")
tm.assert_index_equal(the_int, expected)

def test_intersection_bug(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def test_maybe_convert_i8_nat(self, breaks):
# GH 20636
index = IntervalIndex.from_breaks(breaks)

to_convert = breaks._constructor([pd.NaT] * 3)
to_convert = breaks._constructor([pd.NaT] * 3).as_unit("ns")
expected = Index([np.nan] * 3, dtype=np.float64)
result = index._maybe_convert_i8(to_convert)
tm.assert_index_equal(result, expected)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/period/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_range_slice_outofbounds(self, make_range):
idx = make_range(start="2013/10/01", freq="D", periods=10)

df = DataFrame({"units": [100 + i for i in range(10)]}, index=idx)
empty = DataFrame(index=type(idx)([], freq="D"), columns=["units"])
empty = DataFrame(index=idx[:0], columns=["units"])
empty["units"] = empty["units"].astype("int64")

tm.assert_frame_equal(df["2013/09/01":"2013/09/30"], empty)
Expand Down
12 changes: 7 additions & 5 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,20 +872,22 @@ def test_to_string_truncate_multilevel(self):
with option_context("display.max_rows", 7, "display.max_columns", 7):
assert has_doubly_truncated_repr(df)

def test_truncate_with_different_dtypes(self):
@pytest.mark.parametrize("dtype", ["object", "datetime64[us]"])
def test_truncate_with_different_dtypes(self, dtype):
# 11594, 12045
# when truncated the dtypes of the splits can differ

# 11594
s = Series(
ser = Series(
[datetime(2012, 1, 1)] * 10
+ [datetime(1012, 1, 2)]
+ [datetime(2012, 1, 3)] * 10
+ [datetime(2012, 1, 3)] * 10,
dtype=dtype,
)

with option_context("display.max_rows", 8):
result = str(s)
assert "object" in result
result = str(ser)
assert dtype in result

def test_truncate_with_different_dtypes2(self):
# 12045
Expand Down
21 changes: 7 additions & 14 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,9 +831,7 @@ def test_parse_dates_string(all_parsers):
parser = all_parsers
result = parser.read_csv(StringIO(data), index_col="date", parse_dates=["date"])
# freq doesn't round-trip
index = DatetimeIndex(
list(date_range("1/1/2009", periods=3)), name="date", freq=None
)
index = date_range("1/1/2009", periods=3, name="date")._with_freq(None)

expected = DataFrame(
{"A": ["a", "b", "c"], "B": [1, 3, 4], "C": [2, 4, 5]}, index=index
Expand Down Expand Up @@ -1736,17 +1734,12 @@ def test_parse_timezone(all_parsers):
2018-01-04 09:05:00+09:00,23400"""
result = parser.read_csv(StringIO(data), parse_dates=["dt"])

dti = DatetimeIndex(
list(
date_range(
start="2018-01-04 09:01:00",
end="2018-01-04 09:05:00",
freq="1min",
tz=timezone(timedelta(minutes=540)),
)
),
freq=None,
)
dti = date_range(
start="2018-01-04 09:01:00",
end="2018-01-04 09:05:00",
freq="1min",
tz=timezone(timedelta(minutes=540)),
)._with_freq(None)
expected_data = {"dt": dti, "val": [23350, 23400, 23400, 23400, 23400]}

expected = DataFrame(expected_data)
Expand Down
8 changes: 3 additions & 5 deletions pandas/tests/resample/test_period_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ def test_resample_weekly_all_na(self):
expected = ts.asfreq("W-THU").ffill()
tm.assert_series_equal(result, expected)

def test_resample_tz_localized(self):
dr = date_range(start="2012-4-13", end="2012-5-1")
def test_resample_tz_localized(self, unit):
dr = date_range(start="2012-4-13", end="2012-5-1", unit=unit)
ts = Series(range(len(dr)), index=dr)

ts_utc = ts.tz_localize("UTC")
Expand All @@ -514,9 +514,7 @@ def test_resample_tz_localized(self):
result = ts_local.resample("W").mean()

ts_local_naive = ts_local.copy()
ts_local_naive.index = [
x.replace(tzinfo=None) for x in ts_local_naive.index.to_pydatetime()
]
ts_local_naive.index = ts_local_naive.index.tz_localize(None)

exp = ts_local_naive.resample("W").mean().tz_localize("America/Los_Angeles")
exp.index = pd.DatetimeIndex(exp.index, freq="W")
Expand Down
83 changes: 34 additions & 49 deletions pandas/tests/resample/test_resampler_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,19 @@ def weighted_quantile(series, weights, q):
tm.assert_series_equal(result, expected)


def test_resample_groupby_with_label():
def test_resample_groupby_with_label(unit):
# GH 13235
index = date_range("2000-01-01", freq="2D", periods=5)
index = date_range("2000-01-01", freq="2D", periods=5, unit=unit)
df = DataFrame(index=index, data={"col0": [0, 0, 1, 1, 2], "col1": [1, 1, 1, 1, 1]})
msg = "DataFrameGroupBy.resample operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.groupby("col0").resample("1W", label="left").sum()

mi = [
np.array([0, 0, 1, 2], dtype=np.int64),
pd.to_datetime(
np.array(["1999-12-26", "2000-01-02", "2000-01-02", "2000-01-02"])
np.array(
["1999-12-26", "2000-01-02", "2000-01-02", "2000-01-02"],
dtype=f"M8[{unit}]",
),
]
mindex = pd.MultiIndex.from_arrays(mi, names=["col0", None])
Expand Down Expand Up @@ -505,7 +506,9 @@ def test_resample_groupby_agg_object_dtype_all_nan(consolidate):
idx = pd.MultiIndex.from_arrays(
[
["A"] * 3 + ["B"] * 3,
pd.to_datetime(["2020-01-05", "2020-01-12", "2020-01-19"] * 2),
pd.to_datetime(["2020-01-05", "2020-01-12", "2020-01-19"] * 2).as_unit(
"ns"
),
],
names=["key", "date"],
)
Expand All @@ -530,19 +533,15 @@ def test_groupby_resample_with_list_of_keys():
}
)
result = df.groupby("group").resample("2D", on="date")[["val"]].mean()

mi_exp = pd.MultiIndex.from_arrays(
[[0, 0, 1, 1], df["date"]._values[::2]], names=["group", "date"]
)
expected = DataFrame(
data={
"val": [4.0, 3.5, 6.5, 3.0],
},
index=Index(
data=[
(0, Timestamp("2016-01-01")),
(0, Timestamp("2016-01-03")),
(1, Timestamp("2016-01-05")),
(1, Timestamp("2016-01-07")),
],
name=("group", "date"),
),
index=mi_exp,
)
tm.assert_frame_equal(result, expected)

Expand Down Expand Up @@ -605,17 +604,17 @@ def test_groupby_resample_size_all_index_same():
msg = "DataFrameGroupBy.resample operated on the grouping columns"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = df.groupby("A").resample("D").size()

mi_exp = pd.MultiIndex.from_arrays(
[
[1, 1, 2, 2],
pd.DatetimeIndex(["2000-12-31", "2001-01-01"] * 2, dtype="M8[ns]"),
],
names=["A", None],
)
expected = Series(
3,
index=pd.MultiIndex.from_tuples(
[
(1, Timestamp("2000-12-31")),
(1, Timestamp("2001-01-01")),
(2, Timestamp("2000-12-31")),
(2, Timestamp("2001-01-01")),
],
names=["A", None],
),
index=mi_exp,
)
tm.assert_series_equal(result, expected)

Expand All @@ -627,25 +626,18 @@ def test_groupby_resample_on_index_with_list_of_keys():
"group": [0, 0, 0, 0, 1, 1, 1, 1],
"val": [3, 1, 4, 1, 5, 9, 2, 6],
},
index=Series(
date_range(start="2016-01-01", periods=8),
name="date",
),
index=date_range(start="2016-01-01", periods=8, name="date"),
)
result = df.groupby("group").resample("2D")[["val"]].mean()

mi_exp = pd.MultiIndex.from_arrays(
[[0, 0, 1, 1], df.index[::2]], names=["group", "date"]
)
expected = DataFrame(
data={
"val": [2.0, 2.5, 7.0, 4.0],
},
index=Index(
data=[
(0, Timestamp("2016-01-01")),
(0, Timestamp("2016-01-03")),
(1, Timestamp("2016-01-05")),
(1, Timestamp("2016-01-07")),
],
name=("group", "date"),
),
index=mi_exp,
)
tm.assert_frame_equal(result, expected)

Expand All @@ -659,26 +651,19 @@ def test_groupby_resample_on_index_with_list_of_keys_multi_columns():
"second_val": [2, 7, 1, 8, 2, 8, 1, 8],
"third_val": [1, 4, 1, 4, 2, 1, 3, 5],
},
index=Series(
date_range(start="2016-01-01", periods=8),
name="date",
),
index=date_range(start="2016-01-01", periods=8, name="date"),
)
result = df.groupby("group").resample("2D")[["first_val", "second_val"]].mean()

mi_exp = pd.MultiIndex.from_arrays(
[[0, 0, 1, 1], df.index[::2]], names=["group", "date"]
)
expected = DataFrame(
data={
"first_val": [2.0, 2.5, 7.0, 4.0],
"second_val": [4.5, 4.5, 5.0, 4.5],
},
index=Index(
data=[
(0, Timestamp("2016-01-01")),
(0, Timestamp("2016-01-03")),
(1, Timestamp("2016-01-05")),
(1, Timestamp("2016-01-07")),
],
name=("group", "date"),
),
index=mi_exp,
)
tm.assert_frame_equal(result, expected)

Expand Down
Loading

0 comments on commit 1c606d5

Please sign in to comment.