Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Nov 22, 2023
1 parent 21de024 commit 6835800
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ def maybe_infer_to_datetimelike(
# numpy would have done it for us.
convert_numeric=False,
convert_non_numeric=True,
dtype_if_all_nat=np.dtype("M8[ns]"),
dtype_if_all_nat=np.dtype("M8[s]"),
)


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 @@ -184,6 +184,7 @@ def test_date_range_edges(self, freq):
exp = DatetimeIndex(
[ts + n * td for n in range(1, 5)],
freq=freq,
dtype="M8[ns]",
)
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 @@ -269,7 +269,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 @@ -540,7 +540,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
2 changes: 1 addition & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_constructor_int_dtype_nan(self):
"klass,dtype,na_val",
[
(Index, np.float64, np.nan),
(DatetimeIndex, "datetime64[ns]", pd.NaT),
(DatetimeIndex, "datetime64[s]", pd.NaT),
],
)
def test_index_ctor_infer_nan_nat(self, klass, dtype, na_val):
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/indexes/test_index_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ def test_constructor_infer_nat_dt_like(
)

expected = klass([NaT, NaT])
if dtype[0] == "d":
# we infer all-NaT as second resolution
expected = expected.astype("M8[ns]")
assert expected.dtype == dtype
data = [ctor]
data.insert(pos, nulls_fixture)
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,11 @@ def test_setitem_empty_series(self):

def test_setitem_empty_series_datetimeindex_preserves_freq(self):
# GH#33573 our index should retain its freq
series = Series([], DatetimeIndex([], freq="D"), dtype=object)
dti = DatetimeIndex([], freq="D", dtype="M8[ns]")
series = Series([], index=dti, dtype=object)
key = Timestamp("2012-01-01").as_unit("ns")
series[key] = 47
expected = Series(47, DatetimeIndex([key], freq="D"))
expected = Series(47, DatetimeIndex([key], freq="D").as_unit("ns"))
tm.assert_series_equal(series, expected)
assert series.index.freq == expected.index.freq

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,9 +1169,9 @@ def test_constructor_with_datetime_tz3(self):

def test_constructor_with_datetime_tz2(self):
# with all NaT
s = Series(NaT, index=[0, 1], dtype="datetime64[ns, US/Eastern]")
expected = Series(DatetimeIndex(["NaT", "NaT"], tz="US/Eastern"))
tm.assert_series_equal(s, expected)
ser = Series(NaT, index=[0, 1], dtype="datetime64[ns, US/Eastern]")
expected = Series(DatetimeIndex(["NaT", "NaT"], tz="US/Eastern").as_unit("ns"))
tm.assert_series_equal(ser, expected)

def test_constructor_no_partial_datetime_casting(self):
# GH#40111
Expand Down
27 changes: 14 additions & 13 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ def test_to_datetime_cache_scalar(self):
(
(None, np.nan) + (NaT,) * start_caching_at,
(NaT,) * (start_caching_at + 2),
"ns",
"s",
),
(
(None, Timestamp("2012-07-26")) + (NaT,) * start_caching_at,
Expand Down Expand Up @@ -1604,24 +1604,25 @@ def test_convert_object_to_datetime_with_cache(
(
(
Series([NaT] * 20 + [None] * 20, dtype="object"),
Series([NaT] * 40, dtype="datetime64[ns]"),
Series([NaT] * 40, dtype="datetime64[s]"),
),
(
Series([NaT] * 60 + [None] * 60, dtype="object"),
Series([NaT] * 120, dtype="datetime64[ns]"),
Series([NaT] * 120, dtype="datetime64[s]"),
),
(Series([None] * 20), Series([NaT] * 20, dtype="datetime64[ns]")),
(Series([None] * 60), Series([NaT] * 60, dtype="datetime64[ns]")),
(Series([""] * 20), Series([NaT] * 20, dtype="datetime64[ns]")),
(Series([""] * 60), Series([NaT] * 60, dtype="datetime64[ns]")),
(Series([pd.NA] * 20), Series([NaT] * 20, dtype="datetime64[ns]")),
(Series([pd.NA] * 60), Series([NaT] * 60, dtype="datetime64[ns]")),
(Series([np.nan] * 20), Series([NaT] * 20, dtype="datetime64[ns]")),
(Series([np.nan] * 60), Series([NaT] * 60, dtype="datetime64[ns]")),
(Series([None] * 20), Series([NaT] * 20, dtype="datetime64[s]")),
(Series([None] * 60), Series([NaT] * 60, dtype="datetime64[s]")),
(Series([""] * 20), Series([NaT] * 20, dtype="datetime64[s]")),
(Series([""] * 60), Series([NaT] * 60, dtype="datetime64[s]")),
(Series([pd.NA] * 20), Series([NaT] * 20, dtype="datetime64[s]")),
(Series([pd.NA] * 60), Series([NaT] * 60, dtype="datetime64[s]")),
(Series([np.nan] * 20), Series([NaT] * 20, dtype="datetime64[s]")),
(Series([np.nan] * 60), Series([NaT] * 60, dtype="datetime64[s]")),
),
)
def test_to_datetime_converts_null_like_to_nat(self, cache, input, expected):
# GH35888
expected = Series([NaT] * len(input), dtype="M8[s]")
result = to_datetime(input, cache=cache)
tm.assert_series_equal(result, expected)

Expand Down Expand Up @@ -1977,7 +1978,7 @@ def test_unit_array_mixed_nans_large_int(self, cache):
tm.assert_index_equal(result, expected)

result = to_datetime(values, errors="coerce", unit="s", cache=cache)
expected = DatetimeIndex(["NaT", "NaT", "NaT", "NaT", "NaT"])
expected = DatetimeIndex(["NaT", "NaT", "NaT", "NaT", "NaT"], dtype="M8[ns]")
tm.assert_index_equal(result, expected)

msg = "cannot convert input 1420043460000000000000000 with the unit 's'"
Expand Down Expand Up @@ -3183,7 +3184,7 @@ def test_na_values_with_cache(
self, cache, unique_nulls_fixture, unique_nulls_fixture2
):
# GH22305
expected = Index([NaT, NaT], dtype="datetime64[ns]")
expected = Index([NaT, NaT], dtype="datetime64[s]")
result = to_datetime([unique_nulls_fixture, unique_nulls_fixture2], cache=cache)
tm.assert_index_equal(result, expected)

Expand Down

0 comments on commit 6835800

Please sign in to comment.