Skip to content

Commit 2b09a00

Browse files
committed
Cleanup
1 parent 30cbd6a commit 2b09a00

File tree

5 files changed

+122
-266
lines changed

5 files changed

+122
-266
lines changed

pandas/tests/io/pytables/test_append.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,8 @@ def test_append_misc(setup_path):
696696
with ensure_clean_store(setup_path) as store:
697697
df = DataFrame(
698698
1.1 * np.arange(120).reshape((30, 4)),
699-
columns=Index(
700-
list("ABCD"),
701-
),
702-
index=Index(
703-
[f"i-{i}" for i in range(30)],
704-
),
699+
columns=Index(list("ABCD")),
700+
index=Index([f"i-{i}" for i in range(30)]),
705701
)
706702
store.append("df", df, chunksize=1)
707703
result = store.select("df")
@@ -717,12 +713,8 @@ def test_append_misc_chunksize(setup_path, chunksize):
717713
# more chunksize in append tests
718714
df = DataFrame(
719715
1.1 * np.arange(120).reshape((30, 4)),
720-
columns=Index(
721-
list("ABCD"),
722-
),
723-
index=Index(
724-
[f"i-{i}" for i in range(30)],
725-
),
716+
columns=Index(list("ABCD")),
717+
index=Index([f"i-{i}" for i in range(30)]),
726718
)
727719
df["string"] = "foo"
728720
df["float322"] = 1.0
@@ -765,12 +757,8 @@ def test_append_raise(setup_path, using_infer_string):
765757
# list in column
766758
df = DataFrame(
767759
1.1 * np.arange(120).reshape((30, 4)),
768-
columns=Index(
769-
list("ABCD"),
770-
),
771-
index=Index(
772-
[f"i-{i}" for i in range(30)],
773-
),
760+
columns=Index(list("ABCD")),
761+
index=Index([f"i-{i}" for i in range(30)]),
774762
)
775763
df["invalid"] = [["a"]] * len(df)
776764
assert df.dtypes["invalid"] == np.object_
@@ -822,12 +810,8 @@ def test_append_raise(setup_path, using_infer_string):
822810
# appending an incompatible table
823811
df = DataFrame(
824812
1.1 * np.arange(120).reshape((30, 4)),
825-
columns=Index(
826-
list("ABCD"),
827-
),
828-
index=Index(
829-
[f"i-{i}" for i in range(30)],
830-
),
813+
columns=Index(list("ABCD")),
814+
index=Index([f"i-{i}" for i in range(30)]),
831815
)
832816
store.append("df", df)
833817

@@ -908,9 +892,7 @@ def test_append_with_timedelta(setup_path):
908892
def test_append_to_multiple(setup_path):
909893
df1 = DataFrame(
910894
np.random.default_rng(2).standard_normal((10, 4)),
911-
columns=Index(
912-
list("ABCD"),
913-
),
895+
columns=Index(list("ABCD")),
914896
index=date_range("2000-01-01", periods=10, freq="B"),
915897
)
916898
df2 = df1.copy().rename(columns="{}_2".format)
@@ -947,16 +929,12 @@ def test_append_to_multiple(setup_path):
947929
def test_append_to_multiple_dropna(setup_path):
948930
df1 = DataFrame(
949931
np.random.default_rng(2).standard_normal((10, 4)),
950-
columns=Index(
951-
list("ABCD"),
952-
),
932+
columns=Index(list("ABCD")),
953933
index=date_range("2000-01-01", periods=10, freq="B"),
954934
)
955935
df2 = DataFrame(
956936
np.random.default_rng(2).standard_normal((10, 4)),
957-
columns=Index(
958-
list("ABCD"),
959-
),
937+
columns=Index(list("ABCD")),
960938
index=date_range("2000-01-01", periods=10, freq="B"),
961939
).rename(columns="{}_2".format)
962940
df1.iloc[1, df1.columns.get_indexer(["A", "B"])] = np.nan
@@ -976,9 +954,7 @@ def test_append_to_multiple_dropna(setup_path):
976954
def test_append_to_multiple_dropna_false(setup_path):
977955
df1 = DataFrame(
978956
np.random.default_rng(2).standard_normal((10, 4)),
979-
columns=Index(
980-
list("ABCD"),
981-
),
957+
columns=Index(list("ABCD")),
982958
index=date_range("2000-01-01", periods=10, freq="B"),
983959
)
984960
df2 = df1.copy().rename(columns="{}_2".format)

pandas/tests/io/pytables/test_read.py

Lines changed: 56 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -75,81 +75,75 @@ def test_read_missing_key_opened_store(tmp_path, setup_path):
7575
def test_read_column(setup_path):
7676
df = DataFrame(
7777
np.random.default_rng(2).standard_normal((10, 4)),
78-
columns=Index(
79-
list("ABCD"),
80-
),
78+
columns=Index(list("ABCD")),
8179
index=date_range("2000-01-01", periods=10, freq="B"),
8280
)
8381

8482
with ensure_clean_store(setup_path) as store:
8583
_maybe_remove(store, "df")
8684

87-
# # GH 17912
88-
# # HDFStore.select_column should raise a KeyError
89-
# # exception if the key is not a valid store
90-
# with pytest.raises(KeyError, match="No object named df in the file"):
91-
# store.select_column("df", "index")
92-
#
93-
# store.append("df", df)
94-
# # error
95-
# with pytest.raises(
96-
# KeyError, match=re.escape("'column [foo] not found in the table'")
97-
# ):
98-
# store.select_column("df", "foo")
99-
#
100-
# msg = re.escape("select_column() got an unexpected keyword argument 'where'")
101-
# with pytest.raises(TypeError, match=msg):
102-
# store.select_column("df", "index", where=["index>5"])
103-
#
104-
# # valid
105-
# result = store.select_column("df", "index")
106-
# tm.assert_almost_equal(result.values, Series(df.index).values)
107-
# assert isinstance(result, Series)
108-
#
109-
# # not a data indexable column
110-
# msg = re.escape(
111-
# "column [values_block_0] can not be extracted individually; "
112-
# "it is not data indexable"
113-
# )
114-
# with pytest.raises(ValueError, match=msg):
115-
# store.select_column("df", "values_block_0")
116-
#
117-
# # a data column
118-
# df2 = df.copy()
119-
# df2["string"] = "foo"
120-
# store.append("df2", df2, data_columns=["string"])
121-
# result = store.select_column("df2", "string")
122-
# tm.assert_almost_equal(result.values, df2["string"].values)
85+
# GH 17912
86+
# HDFStore.select_column should raise a KeyError
87+
# exception if the key is not a valid store
88+
with pytest.raises(KeyError, match="No object named df in the file"):
89+
store.select_column("df", "index")
90+
91+
store.append("df", df)
92+
# error
93+
with pytest.raises(
94+
KeyError, match=re.escape("'column [foo] not found in the table'")
95+
):
96+
store.select_column("df", "foo")
97+
98+
msg = re.escape("select_column() got an unexpected keyword argument 'where'")
99+
with pytest.raises(TypeError, match=msg):
100+
store.select_column("df", "index", where=["index>5"])
101+
102+
# valid
103+
result = store.select_column("df", "index")
104+
tm.assert_almost_equal(result.values, Series(df.index).values)
105+
assert isinstance(result, Series)
106+
107+
# not a data indexable column
108+
msg = re.escape(
109+
"column [values_block_0] can not be extracted individually; "
110+
"it is not data indexable"
111+
)
112+
with pytest.raises(ValueError, match=msg):
113+
store.select_column("df", "values_block_0")
114+
115+
# a data column
116+
df2 = df.copy()
117+
df2["string"] = "foo"
118+
store.append("df2", df2, data_columns=["string"])
119+
result = store.select_column("df2", "string")
120+
tm.assert_almost_equal(result.values, df2["string"].values)
123121

124122
# a data column with NaNs, result excludes the NaNs
125123
df3 = df.copy()
126124
df3["string"] = "foo"
127125
df3.loc[df3.index[4:6], "string"] = np.nan
128126
store.append("df3", df3, data_columns=["string"])
129-
# result = store.select_column("df3", "string")
130-
# tm.assert_almost_equal(result.values, df3["string"].values)
131-
#
132-
# # start/stop
133-
# result = store.select_column("df3", "string", start=2)
134-
# tm.assert_almost_equal(result.values, df3["string"].values[2:])
135-
#
136-
# result = store.select_column("df3", "string", start=-2)
137-
# tm.assert_almost_equal(result.values, df3["string"].values[-2:])
138-
#
139-
# result = store.select_column("df3", "string", stop=2)
140-
# tm.assert_almost_equal(result.values, df3["string"].values[:2])
141-
#
142-
# result = store.select_column("df3", "string", stop=-2)
143-
# tm.assert_almost_equal(result.values, df3["string"].values[:-2])
144-
#
145-
# result = store.select_column("df3", "string", start=2, stop=-2)
146-
# tm.assert_almost_equal(result.values, df3["string"].values[2:-2])
147-
148-
print(df3)
149-
print(df3["string"].values)
127+
result = store.select_column("df3", "string")
128+
tm.assert_almost_equal(result.values, df3["string"].values)
129+
130+
# start/stop
131+
result = store.select_column("df3", "string", start=2)
132+
tm.assert_almost_equal(result.values, df3["string"].values[2:])
133+
134+
result = store.select_column("df3", "string", start=-2)
135+
tm.assert_almost_equal(result.values, df3["string"].values[-2:])
136+
137+
result = store.select_column("df3", "string", stop=2)
138+
tm.assert_almost_equal(result.values, df3["string"].values[:2])
139+
140+
result = store.select_column("df3", "string", stop=-2)
141+
tm.assert_almost_equal(result.values, df3["string"].values[:-2])
142+
143+
result = store.select_column("df3", "string", start=2, stop=-2)
144+
tm.assert_almost_equal(result.values, df3["string"].values[2:-2])
145+
150146
result = store.select_column("df3", "string", start=-2, stop=2)
151-
print(result)
152-
print(result.values)
153147
tm.assert_almost_equal(result.values, df3["string"].values[-2:2])
154148

155149
# GH 10392 - make sure column name is preserved

pandas/tests/io/pytables/test_round_trip.py

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,8 @@ def roundtrip(key, obj, **kwargs):
4646

4747
o = DataFrame(
4848
1.1 * np.arange(120).reshape((30, 4)),
49-
columns=Index(
50-
list("ABCD"),
51-
),
52-
index=Index(
53-
[f"i-{i}" for i in range(30)],
54-
),
49+
columns=Index(list("ABCD")),
50+
index=Index([f"i-{i}" for i in range(30)]),
5551
)
5652
tm.assert_frame_equal(o, roundtrip("frame", o))
5753

@@ -151,12 +147,8 @@ def test_api_invalid(tmp_path, setup_path):
151147
# Invalid.
152148
df = DataFrame(
153149
1.1 * np.arange(120).reshape((30, 4)),
154-
columns=Index(
155-
list("ABCD"),
156-
),
157-
index=Index(
158-
[f"i-{i}" for i in range(30)],
159-
),
150+
columns=Index(list("ABCD")),
151+
index=Index([f"i-{i}" for i in range(30)]),
160152
)
161153

162154
msg = "Can only append to Tables"
@@ -288,20 +280,11 @@ def test_series(setup_path):
288280

289281
ts2 = Series(
290282
ts.index,
291-
Index(
292-
ts.index,
293-
),
283+
Index(ts.index),
294284
)
295285
_check_roundtrip(ts2, tm.assert_series_equal, path=setup_path)
296286

297-
ts3 = Series(
298-
ts.values,
299-
Index(
300-
np.asarray(
301-
ts.index,
302-
),
303-
),
304-
)
287+
ts3 = Series(ts.values, Index(np.asarray(ts.index)))
305288
_check_roundtrip(
306289
ts3, tm.assert_series_equal, path=setup_path, check_index_type=False
307290
)
@@ -391,12 +374,8 @@ def test_timeseries_preepoch(setup_path, request):
391374
def test_frame(compression, setup_path):
392375
df = DataFrame(
393376
1.1 * np.arange(120).reshape((30, 4)),
394-
columns=Index(
395-
list("ABCD"),
396-
),
397-
index=Index(
398-
[f"i-{i}" for i in range(30)],
399-
),
377+
columns=Index(list("ABCD")),
378+
index=Index([f"i-{i}" for i in range(30)]),
400379
)
401380

402381
# put in some random NAs
@@ -412,9 +391,7 @@ def test_frame(compression, setup_path):
412391

413392
tdf = DataFrame(
414393
np.random.default_rng(2).standard_normal((10, 4)),
415-
columns=Index(
416-
list("ABCD"),
417-
),
394+
columns=Index(list("ABCD")),
418395
index=date_range("2000-01-01", periods=10, freq="B"),
419396
)
420397
_check_roundtrip(
@@ -493,12 +470,8 @@ def test_store_mixed(compression, setup_path):
493470
def _make_one():
494471
df = DataFrame(
495472
1.1 * np.arange(120).reshape((30, 4)),
496-
columns=Index(
497-
list("ABCD"),
498-
),
499-
index=Index(
500-
[f"i-{i}" for i in range(30)],
501-
),
473+
columns=Index(list("ABCD")),
474+
index=Index([f"i-{i}" for i in range(30)]),
502475
)
503476
df["obj1"] = "foo"
504477
df["obj2"] = "bar"

0 commit comments

Comments
 (0)