Skip to content

TST: more specific freq attrs #33781

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 9 commits into from
Apr 26, 2020
2 changes: 2 additions & 0 deletions pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ def test_constructor_with_datetimelike(self, dtl):

def test_constructor_from_index_series_datetimetz(self):
idx = date_range("2015-01-01 10:00", freq="D", periods=3, tz="US/Eastern")
idx._set_freq(None) # freq not preserved in result.categories
result = Categorical(idx)
tm.assert_index_equal(result.categories, idx)

Expand All @@ -339,6 +340,7 @@ def test_constructor_from_index_series_datetimetz(self):

def test_constructor_from_index_series_timedelta(self):
idx = timedelta_range("1 days", freq="D", periods=3)
idx._set_freq(None) # freq not preserved in result.categories
result = Categorical(idx)
tm.assert_index_equal(result.categories, idx)

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,14 @@ def test_round(self, tz_naive_fixture):

result = dti.round(freq="2T")
expected = dti - pd.Timedelta(minutes=1)
expected._set_freq(None)
tm.assert_index_equal(result, expected)

dta = dti._data
result = dta.round(freq="2T")
expected = expected._data
tm.assert_datetime_array_equal(result, expected)

def test_array_interface(self, datetime_index):
arr = DatetimeArray(datetime_index)

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/frame/methods/test_tz_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def test_tz_convert_and_localize(self, fn):
# GH7846
df2 = DataFrame(np.ones(5), MultiIndex.from_arrays([l0, l1]))

# freq is not preserved in MultiIndex construction
l1_expected._set_freq(None)
l0_expected._set_freq(None)
l1._set_freq(None)
l0._set_freq(None)

df3 = getattr(df2, fn)("US/Pacific", level=0)
assert not df3.index.levels[0].equals(l0)
tm.assert_index_equal(df3.index.levels[0], l0_expected)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/test_axis_select_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def test_reindex(self, float_frame):

# pass non-Index
newFrame = float_frame.reindex(list(datetime_series.index))
tm.assert_index_equal(newFrame.index, datetime_series.index)
expected = datetime_series.index._with_freq(None)
tm.assert_index_equal(newFrame.index, expected)

# copy with no axes
result = float_frame.reindex()
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/frame/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_to_csv_from_csv1(self, float_frame, datetime_frame):
float_frame.to_csv(path, index=False)

# test roundtrip
datetime_frame.index._set_freq(None) # freq does not roundtrip
datetime_frame.to_csv(path)
recons = self.read_csv(path)
tm.assert_frame_equal(datetime_frame, recons)
Expand Down Expand Up @@ -1157,6 +1158,7 @@ def test_to_csv_with_dst_transitions(self):
)

for i in [times, times + pd.Timedelta("10s")]:
i._set_freq(None) # freq is not preserved by read_csv
time_range = np.array(range(len(i)), dtype="int64")
df = DataFrame({"A": time_range}, index=i)
df.to_csv(path, index=True)
Expand All @@ -1170,6 +1172,7 @@ def test_to_csv_with_dst_transitions(self):

# GH11619
idx = pd.date_range("2015-01-01", "2015-12-31", freq="H", tz="Europe/Paris")
idx._set_freq(None) # freq does not round-trip
df = DataFrame({"values": 1, "idx": idx}, index=idx)
with tm.ensure_clean("csv_date_format_with_dst") as path:
df.to_csv(path, index=True)
Expand Down
26 changes: 19 additions & 7 deletions pandas/tests/groupby/test_timegrouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@
import pytz

import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series, Timestamp, date_range
from pandas import (
DataFrame,
DatetimeIndex,
Index,
MultiIndex,
Series,
Timestamp,
date_range,
offsets,
)
import pandas._testing as tm
from pandas.core.groupby.grouper import Grouper
from pandas.core.groupby.ops import BinGrouper
Expand Down Expand Up @@ -243,17 +252,20 @@ def test_timegrouper_with_reg_groups(self):

# single groupers
expected = DataFrame(
{"Quantity": [31], "Date": [datetime(2013, 10, 31, 0, 0)]}
).set_index("Date")
[[31]],
columns=["Quantity"],
index=DatetimeIndex(
[datetime(2013, 10, 31, 0, 0)], freq=offsets.MonthEnd(), name="Date"
),
)
result = df.groupby(pd.Grouper(freq="1M")).sum()
tm.assert_frame_equal(result, expected)

result = df.groupby([pd.Grouper(freq="1M")]).sum()
tm.assert_frame_equal(result, expected)

expected = DataFrame(
{"Quantity": [31], "Date": [datetime(2013, 11, 30, 0, 0)]}
).set_index("Date")
expected.index = expected.index.shift(1)
assert expected.index.freq == offsets.MonthEnd()
result = df.groupby(pd.Grouper(freq="1M", key="Date")).sum()
tm.assert_frame_equal(result, expected)

Expand Down Expand Up @@ -448,7 +460,7 @@ def test_groupby_groups_datetimeindex(self):
for date in dates:
result = grouped.get_group(date)
data = [[df.loc[date, "A"], df.loc[date, "B"]]]
expected_index = pd.DatetimeIndex([date], name="date")
expected_index = pd.DatetimeIndex([date], name="date", freq="D")
expected = pd.DataFrame(data, columns=list("AB"), index=expected_index)
tm.assert_frame_equal(result, expected)

Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/indexes/datetimes/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ def test_astype_with_tz(self):

# GH 18951: tz-naive to tz-aware
idx = date_range("20170101", periods=4)
idx._set_freq(None) # tz_localize does not preserve freq
result = idx.astype("datetime64[ns, US/Eastern]")
expected = date_range("20170101", periods=4, tz="US/Eastern")
expected._set_freq(None)
tm.assert_index_equal(result, expected)

def test_astype_str_compat(self):
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ def test_constructor_dtypes_datetime(self, tz_naive_fixture, attr, klass):
@pytest.mark.parametrize("klass", [pd.Index, pd.TimedeltaIndex])
def test_constructor_dtypes_timedelta(self, attr, klass):
index = pd.timedelta_range("1 days", periods=5)
index._set_freq(None) # wont be preserved by constructors
dtype = index.dtype

values = getattr(index, attr)
Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/series/indexing/test_alter_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def test_reindex_with_datetimes():

result = ts.reindex(list(ts.index[5:10]))
expected = ts[5:10]
expected.index._set_freq(None)
tm.assert_series_equal(result, expected)

result = ts[list(ts.index[5:10])]
Expand All @@ -91,6 +92,7 @@ def test_reindex_corner(datetime_series):

# pass non-Index
reindexed = datetime_series.reindex(list(datetime_series.index))
datetime_series.index._set_freq(None)
tm.assert_series_equal(datetime_series, reindexed)

# bad fill method
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/series/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def test_sort_index_name(self, datetime_series):
assert result.name == datetime_series.name

def test_sort_index(self, datetime_series):
datetime_series.index._set_freq(None)

rindex = list(datetime_series.index)
random.shuffle(rindex)

Expand Down Expand Up @@ -45,6 +47,7 @@ def test_sort_index(self, datetime_series):
random_order.sort_index(level=0, axis=1)

def test_sort_index_inplace(self, datetime_series):
datetime_series.index._set_freq(None)

# For GH#11402
rindex = list(datetime_series.index)
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/series/test_datetime_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def get_dir(s):
exp_values = pd.date_range(
"2015-01-01", "2016-01-01", freq="T", tz="UTC"
).tz_convert("America/Chicago")
exp_values._set_freq(None) # freq not preserved by tz_localize above
expected = Series(exp_values, name="xxx")
tm.assert_series_equal(s, expected)

Expand Down
1 change: 1 addition & 0 deletions pandas/tests/series/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def read_csv(self, path, **kwargs):
return out

def test_from_csv(self, datetime_series, string_series):
datetime_series.index._set_freq(None) # freq doesnt round-trip

with tm.ensure_clean() as path:
datetime_series.to_csv(path, header=False)
Expand Down