Skip to content

Commit ea09d50

Browse files
authored
TST: prepare for freq-checking in tests.io (#33711)
* TST: prepare for freq-checking in tests.io * Update fixture
1 parent 2b6b892 commit ea09d50

10 files changed

+87
-34
lines changed

pandas/tests/io/excel/test_writers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ def test_mixed(self, frame, path):
407407
def test_ts_frame(self, tsframe, path):
408408
df = tsframe
409409

410+
# freq doesnt round-trip
411+
index = pd.DatetimeIndex(np.asarray(df.index), freq=None)
412+
df.index = index
413+
410414
df.to_excel(path, "test1")
411415
reader = ExcelFile(path)
412416

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

478482
def test_sheets(self, frame, tsframe, path):
483+
484+
# freq doesnt round-trip
485+
index = pd.DatetimeIndex(np.asarray(tsframe.index), freq=None)
486+
tsframe.index = index
487+
479488
frame = frame.copy()
480489
frame["A"][:5] = np.nan
481490

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

582591
def test_excel_roundtrip_datetime(self, merge_cells, tsframe, path):
583592
# datetime.date, not sure what to test here exactly
593+
594+
# freq does not round-trip
595+
index = pd.DatetimeIndex(np.asarray(tsframe.index), freq=None)
596+
tsframe.index = index
597+
584598
tsf = tsframe.copy()
585599

586600
tsf.index = [x.date() for x in tsframe.index]

pandas/tests/io/json/test_pandas.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ def setup(self):
4242

4343
yield
4444

45+
@pytest.fixture
46+
def datetime_series(self):
47+
# Same as usual datetime_series, but with index freq set to None,
48+
# since that doesnt round-trip, see GH#33711
49+
ser = tm.makeTimeSeries()
50+
ser.name = "ts"
51+
ser.index = ser.index._with_freq(None)
52+
return ser
53+
54+
@pytest.fixture
55+
def datetime_frame(self):
56+
# Same as usual datetime_frame, but with index freq set to None,
57+
# since that doesnt round-trip, see GH#33711
58+
df = DataFrame(tm.getTimeSeriesData())
59+
df.index = df.index._with_freq(None)
60+
return df
61+
4562
def test_frame_double_encoded_labels(self, orient):
4663
df = DataFrame(
4764
[["a", "b"], ["c", "d"]],
@@ -416,6 +433,9 @@ def test_frame_mixedtype_orient(self): # GH10289
416433
tm.assert_frame_equal(left, right)
417434

418435
def test_v12_compat(self, datapath):
436+
dti = pd.date_range("2000-01-03", "2000-01-07")
437+
# freq doesnt roundtrip
438+
dti = pd.DatetimeIndex(np.asarray(dti), freq=None)
419439
df = DataFrame(
420440
[
421441
[1.56808523, 0.65727391, 1.81021139, -0.17251653],
@@ -425,7 +445,7 @@ def test_v12_compat(self, datapath):
425445
[0.05951614, -2.69652057, 1.28163262, 0.34703478],
426446
],
427447
columns=["A", "B", "C", "D"],
428-
index=pd.date_range("2000-01-03", "2000-01-07"),
448+
index=dti,
429449
)
430450
df["date"] = pd.Timestamp("19920106 18:21:32.12")
431451
df.iloc[3, df.columns.get_loc("date")] = pd.Timestamp("20130101")
@@ -444,6 +464,9 @@ def test_v12_compat(self, datapath):
444464

445465
def test_blocks_compat_GH9037(self):
446466
index = pd.date_range("20000101", periods=10, freq="H")
467+
# freq doesnt round-trip
468+
index = pd.DatetimeIndex(list(index), freq=None)
469+
447470
df_mixed = DataFrame(
448471
OrderedDict(
449472
float_1=[

pandas/tests/io/json/test_ujson.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,8 @@ def test_index(self):
10111011
def test_datetime_index(self):
10121012
date_unit = "ns"
10131013

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

10171018
decoded = DatetimeIndex(np.array(ujson.decode(encoded)))

pandas/tests/io/parser/test_dtypes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ def test_categorical_coerces_numeric(all_parsers):
299299

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

304305
data = "b\n2017-01-01\n2018-01-01\n2019-01-01"
305306
expected = DataFrame({"b": Categorical(dtype["b"].categories)})

pandas/tests/io/parser/test_parse_dates.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,10 @@ def test_parse_dates_string(all_parsers):
681681
"""
682682
parser = all_parsers
683683
result = parser.read_csv(StringIO(data), index_col="date", parse_dates=["date"])
684-
index = date_range("1/1/2009", periods=3)
685-
index.name = "date"
684+
# freq doesnt round-trip
685+
index = DatetimeIndex(
686+
list(date_range("1/1/2009", periods=3)), name="date", freq=None
687+
)
686688

687689
expected = DataFrame(
688690
{"A": ["a", "b", "c"], "B": [1, 3, 4], "C": [2, 4, 5]}, index=index
@@ -1430,11 +1432,16 @@ def test_parse_timezone(all_parsers):
14301432
2018-01-04 09:05:00+09:00,23400"""
14311433
result = parser.read_csv(StringIO(data), parse_dates=["dt"])
14321434

1433-
dti = pd.date_range(
1434-
start="2018-01-04 09:01:00",
1435-
end="2018-01-04 09:05:00",
1436-
freq="1min",
1437-
tz=pytz.FixedOffset(540),
1435+
dti = pd.DatetimeIndex(
1436+
list(
1437+
pd.date_range(
1438+
start="2018-01-04 09:01:00",
1439+
end="2018-01-04 09:05:00",
1440+
freq="1min",
1441+
tz=pytz.FixedOffset(540),
1442+
),
1443+
),
1444+
freq=None,
14381445
)
14391446
expected_data = {"dt": dti, "val": [23350, 23400, 23400, 23400, 23400]}
14401447

pandas/tests/io/pytables/test_store.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,8 @@ def test_append_frame_column_oriented(self, setup_path):
12311231

12321232
# column oriented
12331233
df = tm.makeTimeDataFrame()
1234+
df.index = df.index._with_freq(None) # freq doesnt round-trip
1235+
12341236
_maybe_remove(store, "df1")
12351237
store.append("df1", df.iloc[:, :2], axes=["columns"])
12361238
store.append("df1", df.iloc[:, 2:])

pandas/tests/io/pytables/test_timezones.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,11 @@ def test_append_with_timezones_dateutil(setup_path):
106106
# as index
107107
with ensure_clean_store(setup_path) as store:
108108

109+
dti = date_range("2000-1-1", periods=3, freq="H", tz=gettz("US/Eastern"))
110+
dti = dti._with_freq(None) # freq doesnt round-trip
111+
109112
# GH 4098 example
110-
df = DataFrame(
111-
dict(
112-
A=Series(
113-
range(3),
114-
index=date_range(
115-
"2000-1-1", periods=3, freq="H", tz=gettz("US/Eastern")
116-
),
117-
)
118-
)
119-
)
113+
df = DataFrame(dict(A=Series(range(3), index=dti,)))
120114

121115
_maybe_remove(store, "df")
122116
store.put("df", df)
@@ -199,15 +193,11 @@ def test_append_with_timezones_pytz(setup_path):
199193
# as index
200194
with ensure_clean_store(setup_path) as store:
201195

196+
dti = date_range("2000-1-1", periods=3, freq="H", tz="US/Eastern")
197+
dti = dti._with_freq(None) # freq doesnt round-trip
198+
202199
# GH 4098 example
203-
df = DataFrame(
204-
dict(
205-
A=Series(
206-
range(3),
207-
index=date_range("2000-1-1", periods=3, freq="H", tz="US/Eastern"),
208-
)
209-
)
210-
)
200+
df = DataFrame(dict(A=Series(range(3), index=dti,)))
211201

212202
_maybe_remove(store, "df")
213203
store.put("df", df)
@@ -258,6 +248,7 @@ def test_timezones_fixed(setup_path):
258248

259249
# index
260250
rng = date_range("1/1/2000", "1/30/2000", tz="US/Eastern")
251+
rng = rng._with_freq(None) # freq doesnt round-trip
261252
df = DataFrame(np.random.randn(len(rng), 4), index=rng)
262253
store["df"] = df
263254
result = store["df"]
@@ -346,6 +337,7 @@ def test_dst_transitions(setup_path):
346337
freq="H",
347338
ambiguous="infer",
348339
)
340+
times = times._with_freq(None) # freq doesnt round-trip
349341

350342
for i in [times, times + pd.Timedelta("10min")]:
351343
_maybe_remove(store, "df")

pandas/tests/io/test_feather.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,21 @@ def test_basic(self):
6363
"bool": [True, False, True],
6464
"bool_with_null": [True, np.nan, False],
6565
"cat": pd.Categorical(list("abc")),
66-
"dt": pd.date_range("20130101", periods=3),
67-
"dttz": pd.date_range("20130101", periods=3, tz="US/Eastern"),
66+
"dt": pd.DatetimeIndex(
67+
list(pd.date_range("20130101", periods=3)), freq=None
68+
),
69+
"dttz": pd.DatetimeIndex(
70+
list(pd.date_range("20130101", periods=3, tz="US/Eastern")),
71+
freq=None,
72+
),
6873
"dt_with_null": [
6974
pd.Timestamp("20130101"),
7075
pd.NaT,
7176
pd.Timestamp("20130103"),
7277
],
73-
"dtns": pd.date_range("20130101", periods=3, freq="ns"),
78+
"dtns": pd.DatetimeIndex(
79+
list(pd.date_range("20130101", periods=3, freq="ns")), freq=None,
80+
),
7481
}
7582
)
7683
if pyarrow_version >= LooseVersion("0.16.1.dev"):

pandas/tests/io/test_parquet.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ def test_write_index(self, engine):
385385
# non-default index
386386
for index in indexes:
387387
df.index = index
388+
if isinstance(index, pd.DatetimeIndex):
389+
index._set_freq(None) # freq doesnt round-trip
388390
check_round_trip(df, engine, check_names=check_names)
389391

390392
# index with meta-data
@@ -462,7 +464,9 @@ def test_basic(self, pa, df_full):
462464
df = df_full
463465

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

468472
check_round_trip(df, pa)
@@ -629,7 +633,9 @@ class TestParquetFastParquet(Base):
629633
def test_basic(self, fp, df_full):
630634
df = df_full
631635

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

pandas/tests/io/test_sql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ def test_out_of_bounds_datetime(self):
14911491
def test_naive_datetimeindex_roundtrip(self):
14921492
# GH 23510
14931493
# Ensure that a naive DatetimeIndex isn't converted to UTC
1494-
dates = date_range("2018-01-01", periods=5, freq="6H")
1494+
dates = date_range("2018-01-01", periods=5, freq="6H")._with_freq(None)
14951495
expected = DataFrame({"nums": range(5)}, index=dates)
14961496
expected.to_sql("foo_table", self.conn, index_label="info_date")
14971497
result = sql.read_sql_table("foo_table", self.conn, index_col="info_date")

0 commit comments

Comments
 (0)