Skip to content

TST: more accurate freq attr in expecteds #33773

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

Merged
merged 3 commits into from
Apr 24, 2020
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
4 changes: 4 additions & 0 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,7 @@ def test_dti_add_tdi(self, tz_naive_fixture):
dti = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10)
tdi = pd.timedelta_range("0 days", periods=10)
expected = pd.date_range("2017-01-01", periods=10, tz=tz)
expected._set_freq(None)

# add with TimdeltaIndex
result = dti + tdi
Expand All @@ -2073,6 +2074,7 @@ def test_dti_iadd_tdi(self, tz_naive_fixture):
dti = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10)
tdi = pd.timedelta_range("0 days", periods=10)
expected = pd.date_range("2017-01-01", periods=10, tz=tz)
expected._set_freq(None)

# iadd with TimdeltaIndex
result = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10)
Expand All @@ -2098,6 +2100,7 @@ def test_dti_sub_tdi(self, tz_naive_fixture):
dti = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10)
tdi = pd.timedelta_range("0 days", periods=10)
expected = pd.date_range("2017-01-01", periods=10, tz=tz, freq="-1D")
expected = expected._with_freq(None)

# sub with TimedeltaIndex
result = dti - tdi
Expand All @@ -2121,6 +2124,7 @@ def test_dti_isub_tdi(self, tz_naive_fixture):
dti = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10)
tdi = pd.timedelta_range("0 days", periods=10)
expected = pd.date_range("2017-01-01", periods=10, tz=tz, freq="-1D")
expected = expected._with_freq(None)

# isub with TimedeltaIndex
result = DatetimeIndex([Timestamp("2017-01-01", tz=tz)] * 10)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def test_numeric_arr_mul_tdscalar(self, scalar_td, numeric_idx, box):
# GH#19333
index = numeric_idx

expected = pd.timedelta_range("0 days", "4 days")
expected = pd.TimedeltaIndex([pd.Timedelta(days=n) for n in range(5)])

index = tm.box_expected(index, box)
expected = tm.box_expected(expected, box)
Expand Down Expand Up @@ -974,7 +974,7 @@ def check(series, other):
tm.assert_almost_equal(np.asarray(result), expected)

assert result.name == series.name
tm.assert_index_equal(result.index, series.index)
tm.assert_index_equal(result.index, series.index._with_freq(None))

tser = tm.makeTimeSeries().rename("ts")
check(tser, tser * 2)
Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,7 @@ def _check(result, expected):
_check(result, expected)

result = dti_tz - td
expected = DatetimeIndex(
["20121231", "20130101", "20130102"], tz="US/Eastern", freq="D"
)
expected = DatetimeIndex(["20121231", "20130101", "20130102"], tz="US/Eastern")
tm.assert_index_equal(result, expected)

def test_dti_tdi_numeric_ops(self):
Expand Down Expand Up @@ -515,7 +513,13 @@ def test_timedelta(self, freq):
result2 = DatetimeIndex(s - np.timedelta64(100000000))
result3 = rng - np.timedelta64(100000000)
result4 = DatetimeIndex(s - pd.offsets.Hour(1))

assert result1.freq == rng.freq
result1 = result1._with_freq(None)
tm.assert_index_equal(result1, result4)

assert result3.freq == rng.freq
result3 = result3._with_freq(None)
tm.assert_index_equal(result2, result3)

def test_tda_add_sub_index(self):
Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ def test_ensure_copied_data(self, indices):
result = index_type(indices.values, copy=True, **init_kwargs)
if is_datetime64tz_dtype(indices.dtype):
result = result.tz_localize("UTC").tz_convert(indices.tz)
if isinstance(indices, (DatetimeIndex, TimedeltaIndex)):
indices._set_freq(None)

tm.assert_index_equal(indices, result)

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ def test_factorize_tz(self, tz_naive_fixture):
for obj in [idx, pd.Series(idx)]:
arr, res = obj.factorize()
tm.assert_numpy_array_equal(arr, exp_arr)
tm.assert_index_equal(res, base)
expected = base._with_freq(None)
tm.assert_index_equal(res, expected)

def test_factorize_dst(self):
# GH 13750
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/indexes/datetimes/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,14 @@ def test_value_counts_unique(self, tz_naive_fixture):

exp_idx = pd.date_range("2011-01-01 18:00", freq="-1H", periods=10, tz=tz)
expected = Series(range(10, 0, -1), index=exp_idx, dtype="int64")
expected.index._set_freq(None)

for obj in [idx, Series(idx)]:

tm.assert_series_equal(obj.value_counts(), expected)

expected = pd.date_range("2011-01-01 09:00", freq="H", periods=10, tz=tz)
expected = expected._with_freq(None)
tm.assert_index_equal(idx.unique(), expected)

idx = DatetimeIndex(
Expand Down Expand Up @@ -274,7 +277,8 @@ def test_drop_duplicates_metadata(self, freq_sample):
idx_dup = idx.append(idx)
assert idx_dup.freq is None # freq is reset
result = idx_dup.drop_duplicates()
tm.assert_index_equal(idx, result)
expected = idx._with_freq(None)
tm.assert_index_equal(result, expected)
assert result.freq is None

@pytest.mark.parametrize(
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,17 @@ def test_intersection_empty(self, tz_aware_fixture, freq):
assert result.freq == rng.freq

# no overlap GH#33604
check_freq = freq != "T" # We don't preserve freq on non-anchored offsets
result = rng[:3].intersection(rng[-3:])
tm.assert_index_equal(result, rng[:0])
if freq != "T":
if check_freq:
# We don't preserve freq on non-anchored offsets
assert result.freq == rng.freq

# swapped left and right
result = rng[-3:].intersection(rng[:3])
tm.assert_index_equal(result, rng[:0])
if freq != "T":
if check_freq:
# We don't preserve freq on non-anchored offsets
assert result.freq == rng.freq

Expand Down
36 changes: 21 additions & 15 deletions pandas/tests/indexes/datetimes/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def test_tz_convert_roundtrip(self, tz_aware_fixture):
tm.assert_index_equal(reset, expected)
assert reset.tzinfo is None
expected = converted.tz_convert("UTC").tz_localize(None)
expected = expected._with_freq("infer")
tm.assert_index_equal(reset, expected)

def test_dti_tz_convert_tzlocal(self):
Expand Down Expand Up @@ -352,8 +353,9 @@ def test_dti_tz_localize_ambiguous_infer(self, tz):
]
di = DatetimeIndex(times)
localized = di.tz_localize(tz, ambiguous="infer")
tm.assert_index_equal(dr, localized)
tm.assert_index_equal(dr, DatetimeIndex(times, tz=tz, ambiguous="infer"))
expected = dr._with_freq(None)
tm.assert_index_equal(expected, localized)
tm.assert_index_equal(expected, DatetimeIndex(times, tz=tz, ambiguous="infer"))

# When there is no dst transition, nothing special happens
dr = date_range(datetime(2011, 6, 1, 0), periods=10, freq=pd.offsets.Hour())
Expand Down Expand Up @@ -458,15 +460,16 @@ def test_dti_tz_localize_roundtrip(self, tz_aware_fixture):
localized.tz_localize(tz)
reset = localized.tz_localize(None)
assert reset.tzinfo is None
tm.assert_index_equal(reset, idx)
expected = idx._with_freq(None)
tm.assert_index_equal(reset, expected)

def test_dti_tz_localize_naive(self):
rng = date_range("1/1/2011", periods=100, freq="H")

conv = rng.tz_localize("US/Pacific")
exp = date_range("1/1/2011", periods=100, freq="H", tz="US/Pacific")

tm.assert_index_equal(conv, exp)
tm.assert_index_equal(conv, exp._with_freq(None))

def test_dti_tz_localize_tzlocal(self):
# GH#13583
Expand Down Expand Up @@ -526,8 +529,9 @@ def test_dti_tz_localize_ambiguous_flags(self, tz):
di = DatetimeIndex(times)
is_dst = [1, 1, 0, 0, 0]
localized = di.tz_localize(tz, ambiguous=is_dst)
tm.assert_index_equal(dr, localized)
tm.assert_index_equal(dr, DatetimeIndex(times, tz=tz, ambiguous=is_dst))
expected = dr._with_freq(None)
tm.assert_index_equal(expected, localized)
tm.assert_index_equal(expected, DatetimeIndex(times, tz=tz, ambiguous=is_dst))

localized = di.tz_localize(tz, ambiguous=np.array(is_dst))
tm.assert_index_equal(dr, localized)
Expand Down Expand Up @@ -703,9 +707,9 @@ def test_dti_tz_localize_nonexistent_shift_invalid(self, offset, tz_type):
def test_normalize_tz(self):
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz="US/Eastern")

result = rng.normalize()
result = rng.normalize() # does not preserve freq
expected = date_range("1/1/2000", periods=10, freq="D", tz="US/Eastern")
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected._with_freq(None))

assert result.is_normalized
assert not rng.is_normalized
Expand All @@ -720,9 +724,9 @@ def test_normalize_tz(self):
assert not rng.is_normalized

rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz=tzlocal())
result = rng.normalize()
result = rng.normalize() # does not preserve freq
expected = date_range("1/1/2000", periods=10, freq="D", tz=tzlocal())
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected._with_freq(None))

assert result.is_normalized
assert not rng.is_normalized
Expand All @@ -746,6 +750,7 @@ def test_normalize_tz_local(self, timezone):

result = rng.normalize()
expected = date_range("1/1/2000", periods=10, freq="D", tz=tzlocal())
expected = expected._with_freq(None)
tm.assert_index_equal(result, expected)

assert result.is_normalized
Expand Down Expand Up @@ -777,10 +782,8 @@ def test_dti_constructor_with_fixed_tz(self):
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
def test_dti_convert_datetime_list(self, tzstr):
dr = date_range("2012-06-02", periods=10, tz=tzstr, name="foo")
dr2 = DatetimeIndex(list(dr), name="foo")
dr2 = DatetimeIndex(list(dr), name="foo", freq="D")
tm.assert_index_equal(dr, dr2)
assert dr.tz == dr2.tz
assert dr2.name == "foo"

def test_dti_construction_univalent(self):
rng = date_range("03/12/2012 00:00", periods=10, freq="W-FRI", tz="US/Eastern")
Expand All @@ -803,6 +806,7 @@ def test_dti_tz_constructors(self, tzstr):

idx1 = to_datetime(arr).tz_localize(tzstr)
idx2 = pd.date_range(start="2005-11-10 08:00:00", freq="H", periods=2, tz=tzstr)
idx2 = idx2._with_freq(None) # the others all have freq=None
idx3 = DatetimeIndex(arr, tz=tzstr)
idx4 = DatetimeIndex(np.array(arr), tz=tzstr)

Expand Down Expand Up @@ -913,7 +917,7 @@ def test_date_range_localize(self):
rng3 = date_range("3/11/2012 03:00", periods=15, freq="H")
rng3 = rng3.tz_localize("US/Eastern")

tm.assert_index_equal(rng, rng3)
tm.assert_index_equal(rng._with_freq(None), rng3)

# DST transition time
val = rng[0]
Expand All @@ -926,7 +930,9 @@ def test_date_range_localize(self):

# Right before the DST transition
rng = date_range("3/11/2012 00:00", periods=2, freq="H", tz="US/Eastern")
rng2 = DatetimeIndex(["3/11/2012 00:00", "3/11/2012 01:00"], tz="US/Eastern")
rng2 = DatetimeIndex(
["3/11/2012 00:00", "3/11/2012 01:00"], tz="US/Eastern", freq="H"
)
tm.assert_index_equal(rng, rng2)
exp = Timestamp("3/11/2012 00:00", tz="US/Eastern")
assert exp.hour == 0
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/timedeltas/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ def test_drop_duplicates_metadata(self, freq_sample):
idx_dup = idx.append(idx)
assert idx_dup.freq is None # freq is reset
result = idx_dup.drop_duplicates()
tm.assert_index_equal(idx, result)
expected = idx._with_freq(None)
tm.assert_index_equal(expected, result)
assert result.freq is None

@pytest.mark.parametrize(
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ def test_indexing_with_datetimeindex_tz(self):

for sel in (index, list(index)):
# getitem
tm.assert_series_equal(ser[sel], ser)
result = ser[sel]
expected = ser
if sel is not index:
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(result, expected)

# setitem
result = ser.copy()
Expand All @@ -154,7 +158,8 @@ def test_indexing_with_datetimeindex_tz(self):
tm.assert_series_equal(result, expected)

# .loc getitem
tm.assert_series_equal(ser.loc[sel], ser)
result = ser.loc[sel]
tm.assert_series_equal(result, ser)

# .loc setitem
result = ser.copy()
Expand Down Expand Up @@ -226,6 +231,7 @@ def test_series_partial_set_datetime(self):

result = ser.loc[[Timestamp("2011-01-01"), Timestamp("2011-01-02")]]
exp = Series([0.1, 0.2], index=idx, name="s")
exp.index = exp.index._with_freq(None)
tm.assert_series_equal(result, exp, check_index_type=True)

keys = [
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_partial_setting(self):
)

expected = pd.concat(
[df_orig, DataFrame({"A": 7}, index=[dates[-1] + dates.freq])], sort=True
[df_orig, DataFrame({"A": 7}, index=dates[-1:] + dates.freq)], sort=True
)
df = df_orig.copy()
df.loc[dates[-1] + dates.freq, "A"] = 7
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/series/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,18 @@ def test_sort_index_inplace(self, datetime_series):
result = random_order.sort_index(ascending=False, inplace=True)

assert result is None
tm.assert_series_equal(
random_order, datetime_series.reindex(datetime_series.index[::-1])
)
expected = datetime_series.reindex(datetime_series.index[::-1])
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(random_order, expected)

# ascending
random_order = datetime_series.reindex(rindex)
result = random_order.sort_index(ascending=True, inplace=True)

assert result is None
tm.assert_series_equal(random_order, datetime_series)
expected = datetime_series.copy()
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(random_order, expected)

def test_sort_index_level(self):
mi = MultiIndex.from_tuples([[1, 1, 3], [1, 1, 1]], names=list("ABC"))
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/series/methods/test_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ class TestSeriesToDict:
)
def test_to_dict(self, mapping, datetime_series):
# GH#16122
tm.assert_series_equal(
Series(datetime_series.to_dict(mapping), name="ts"), datetime_series
)
result = Series(datetime_series.to_dict(mapping), name="ts")
expected = datetime_series.copy()
expected.index = expected.index._with_freq(None)
tm.assert_series_equal(result, expected)

from_method = Series(datetime_series.to_dict(collections.Counter))
from_constructor = Series(collections.Counter(datetime_series.items()))
tm.assert_series_equal(from_method, from_constructor)
1 change: 1 addition & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,7 @@ def test_set_index_datetime(self):
tz="US/Eastern",
)
idx3 = pd.date_range("2011-01-01 09:00", periods=6, tz="Asia/Tokyo")
idx3._set_freq(None)

df = df.set_index(idx1)
df = df.set_index(idx2, append=True)
Expand Down