Skip to content

TST: prepare for freq-checking in tests.io #33711

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
14 changes: 14 additions & 0 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ def test_mixed(self, frame, path):
def test_ts_frame(self, tsframe, path):
df = tsframe

# freq doesnt round-trip
index = pd.DatetimeIndex(np.asarray(df.index), freq=None)
df.index = index

df.to_excel(path, "test1")
reader = ExcelFile(path)

Expand Down Expand Up @@ -476,6 +480,11 @@ def test_inf_roundtrip(self, path):
tm.assert_frame_equal(df, recons)

def test_sheets(self, frame, tsframe, path):

# freq doesnt round-trip
index = pd.DatetimeIndex(np.asarray(tsframe.index), freq=None)
tsframe.index = index

frame = frame.copy()
frame["A"][:5] = np.nan

Expand Down Expand Up @@ -581,6 +590,11 @@ def test_excel_roundtrip_indexname(self, merge_cells, path):

def test_excel_roundtrip_datetime(self, merge_cells, tsframe, path):
# datetime.date, not sure what to test here exactly

# freq does not round-trip
index = pd.DatetimeIndex(np.asarray(tsframe.index), freq=None)
tsframe.index = index

tsf = tsframe.copy()

tsf.index = [x.date() for x in tsframe.index]
Expand Down
25 changes: 24 additions & 1 deletion pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ def setup(self):

yield

@pytest.fixture
def datetime_series(self):
# Same as usual datetime_series, but with index freq set to None,
# since that doesnt round-trip, see GH#33711
ser = tm.makeTimeSeries()
ser.name = "ts"
ser.index = ser.index._with_freq(None)
return ser

@pytest.fixture
def datetime_frame(self):
# Same as usual datetime_frame, but with index freq set to None,
# since that doesnt round-trip, see GH#33711
df = DataFrame(tm.getTimeSeriesData())
df.index = df.index._with_freq(None)
return df

def test_frame_double_encoded_labels(self, orient):
df = DataFrame(
[["a", "b"], ["c", "d"]],
Expand Down Expand Up @@ -416,6 +433,9 @@ def test_frame_mixedtype_orient(self): # GH10289
tm.assert_frame_equal(left, right)

def test_v12_compat(self, datapath):
dti = pd.date_range("2000-01-03", "2000-01-07")
# freq doesnt roundtrip
dti = pd.DatetimeIndex(np.asarray(dti), freq=None)
df = DataFrame(
[
[1.56808523, 0.65727391, 1.81021139, -0.17251653],
Expand All @@ -425,7 +445,7 @@ def test_v12_compat(self, datapath):
[0.05951614, -2.69652057, 1.28163262, 0.34703478],
],
columns=["A", "B", "C", "D"],
index=pd.date_range("2000-01-03", "2000-01-07"),
index=dti,
)
df["date"] = pd.Timestamp("19920106 18:21:32.12")
df.iloc[3, df.columns.get_loc("date")] = pd.Timestamp("20130101")
Expand All @@ -444,6 +464,9 @@ def test_v12_compat(self, datapath):

def test_blocks_compat_GH9037(self):
index = pd.date_range("20000101", periods=10, freq="H")
# freq doesnt round-trip
index = pd.DatetimeIndex(list(index), freq=None)

df_mixed = DataFrame(
OrderedDict(
float_1=[
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/io/json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,8 @@ def test_index(self):
def test_datetime_index(self):
date_unit = "ns"

rng = date_range("1/1/2000", periods=20)
# freq doesnt round-trip
rng = DatetimeIndex(list(date_range("1/1/2000", periods=20)), freq=None)
encoded = ujson.encode(rng, date_unit=date_unit)

decoded = DatetimeIndex(np.array(ujson.decode(encoded)))
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/io/parser/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ def test_categorical_coerces_numeric(all_parsers):

def test_categorical_coerces_datetime(all_parsers):
parser = all_parsers
dtype = {"b": CategoricalDtype(pd.date_range("2017", "2019", freq="AS"))}
dti = pd.DatetimeIndex(["2017-01-01", "2018-01-01", "2019-01-01"], freq=None)
dtype = {"b": CategoricalDtype(dti)}

data = "b\n2017-01-01\n2018-01-01\n2019-01-01"
expected = DataFrame({"b": Categorical(dtype["b"].categories)})
Expand Down
21 changes: 14 additions & 7 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,10 @@ def test_parse_dates_string(all_parsers):
"""
parser = all_parsers
result = parser.read_csv(StringIO(data), index_col="date", parse_dates=["date"])
index = date_range("1/1/2009", periods=3)
index.name = "date"
# freq doesnt round-trip
index = DatetimeIndex(
list(date_range("1/1/2009", periods=3)), name="date", freq=None
)

expected = DataFrame(
{"A": ["a", "b", "c"], "B": [1, 3, 4], "C": [2, 4, 5]}, index=index
Expand Down Expand Up @@ -1430,11 +1432,16 @@ 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 = pd.date_range(
start="2018-01-04 09:01:00",
end="2018-01-04 09:05:00",
freq="1min",
tz=pytz.FixedOffset(540),
dti = pd.DatetimeIndex(
list(
pd.date_range(
start="2018-01-04 09:01:00",
end="2018-01-04 09:05:00",
freq="1min",
tz=pytz.FixedOffset(540),
),
),
freq=None,
)
expected_data = {"dt": dti, "val": [23350, 23400, 23400, 23400, 23400]}

Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/io/pytables/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,8 @@ def test_append_frame_column_oriented(self, setup_path):

# column oriented
df = tm.makeTimeDataFrame()
df.index = df.index._with_freq(None) # freq doesnt round-trip

_maybe_remove(store, "df1")
store.append("df1", df.iloc[:, :2], axes=["columns"])
store.append("df1", df.iloc[:, 2:])
Expand Down
28 changes: 10 additions & 18 deletions pandas/tests/io/pytables/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,11 @@ def test_append_with_timezones_dateutil(setup_path):
# as index
with ensure_clean_store(setup_path) as store:

dti = date_range("2000-1-1", periods=3, freq="H", tz=gettz("US/Eastern"))
dti = dti._with_freq(None) # freq doesnt round-trip

# GH 4098 example
df = DataFrame(
dict(
A=Series(
range(3),
index=date_range(
"2000-1-1", periods=3, freq="H", tz=gettz("US/Eastern")
),
)
)
)
df = DataFrame(dict(A=Series(range(3), index=dti,)))

_maybe_remove(store, "df")
store.put("df", df)
Expand Down Expand Up @@ -199,15 +193,11 @@ def test_append_with_timezones_pytz(setup_path):
# as index
with ensure_clean_store(setup_path) as store:

dti = date_range("2000-1-1", periods=3, freq="H", tz="US/Eastern")
dti = dti._with_freq(None) # freq doesnt round-trip

# GH 4098 example
df = DataFrame(
dict(
A=Series(
range(3),
index=date_range("2000-1-1", periods=3, freq="H", tz="US/Eastern"),
)
)
)
df = DataFrame(dict(A=Series(range(3), index=dti,)))

_maybe_remove(store, "df")
store.put("df", df)
Expand Down Expand Up @@ -258,6 +248,7 @@ def test_timezones_fixed(setup_path):

# index
rng = date_range("1/1/2000", "1/30/2000", tz="US/Eastern")
rng = rng._with_freq(None) # freq doesnt round-trip
df = DataFrame(np.random.randn(len(rng), 4), index=rng)
store["df"] = df
result = store["df"]
Expand Down Expand Up @@ -346,6 +337,7 @@ def test_dst_transitions(setup_path):
freq="H",
ambiguous="infer",
)
times = times._with_freq(None) # freq doesnt round-trip

for i in [times, times + pd.Timedelta("10min")]:
_maybe_remove(store, "df")
Expand Down
13 changes: 10 additions & 3 deletions pandas/tests/io/test_feather.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,21 @@ def test_basic(self):
"bool": [True, False, True],
"bool_with_null": [True, np.nan, False],
"cat": pd.Categorical(list("abc")),
"dt": pd.date_range("20130101", periods=3),
"dttz": pd.date_range("20130101", periods=3, tz="US/Eastern"),
"dt": pd.DatetimeIndex(
list(pd.date_range("20130101", periods=3)), freq=None
),
"dttz": pd.DatetimeIndex(
list(pd.date_range("20130101", periods=3, tz="US/Eastern")),
freq=None,
),
"dt_with_null": [
pd.Timestamp("20130101"),
pd.NaT,
pd.Timestamp("20130103"),
],
"dtns": pd.date_range("20130101", periods=3, freq="ns"),
"dtns": pd.DatetimeIndex(
list(pd.date_range("20130101", periods=3, freq="ns")), freq=None,
),
}
)
if pyarrow_version >= LooseVersion("0.16.1.dev"):
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/io/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ def test_write_index(self, engine):
# non-default index
for index in indexes:
df.index = index
if isinstance(index, pd.DatetimeIndex):
index._set_freq(None) # freq doesnt round-trip
check_round_trip(df, engine, check_names=check_names)

# index with meta-data
Expand Down Expand Up @@ -462,7 +464,9 @@ def test_basic(self, pa, df_full):
df = df_full

# additional supported types for pyarrow
df["datetime_tz"] = pd.date_range("20130101", periods=3, tz="Europe/Brussels")
dti = pd.date_range("20130101", periods=3, tz="Europe/Brussels")
dti._set_freq(None) # freq doesnt round-trip
df["datetime_tz"] = dti
df["bool_with_none"] = [True, None, True]

check_round_trip(df, pa)
Expand Down Expand Up @@ -629,7 +633,9 @@ class TestParquetFastParquet(Base):
def test_basic(self, fp, df_full):
df = df_full

df["datetime_tz"] = pd.date_range("20130101", periods=3, tz="US/Eastern")
dti = pd.date_range("20130101", periods=3, tz="US/Eastern")
dti._set_freq(None) # freq doesnt round-trip
df["datetime_tz"] = dti
df["timedelta"] = pd.timedelta_range("1 day", periods=3)
check_round_trip(df, fp)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ def test_out_of_bounds_datetime(self):
def test_naive_datetimeindex_roundtrip(self):
# GH 23510
# Ensure that a naive DatetimeIndex isn't converted to UTC
dates = date_range("2018-01-01", periods=5, freq="6H")
dates = date_range("2018-01-01", periods=5, freq="6H")._with_freq(None)
expected = DataFrame({"nums": range(5)}, index=dates)
expected.to_sql("foo_table", self.conn, index_label="info_date")
result = sql.read_sql_table("foo_table", self.conn, index_col="info_date")
Expand Down