Skip to content

Commit 2c4c072

Browse files
authored
DEPR: Positional arguments in to_hdf except path_or_buf (#54491)
* Adding deprecation, updating unit tests and documentation. * Fixing failing unit test. * Moving contribution to whatsnew/v2.2.0 * Update v2.1.0.rst
1 parent 7ff13b4 commit 2c4c072

20 files changed

+135
-119
lines changed

doc/source/user_guide/io.rst

+8-8
Original file line numberDiff line numberDiff line change
@@ -4220,7 +4220,7 @@ similar to how ``read_csv`` and ``to_csv`` work.
42204220
.. ipython:: python
42214221
42224222
df_tl = pd.DataFrame({"A": list(range(5)), "B": list(range(5))})
4223-
df_tl.to_hdf("store_tl.h5", "table", append=True)
4223+
df_tl.to_hdf("store_tl.h5", key="table", append=True)
42244224
pd.read_hdf("store_tl.h5", "table", where=["index>2"])
42254225
42264226
.. ipython:: python
@@ -4243,12 +4243,12 @@ HDFStore will by default not drop rows that are all missing. This behavior can b
42434243
)
42444244
df_with_missing
42454245
4246-
df_with_missing.to_hdf("file.h5", "df_with_missing", format="table", mode="w")
4246+
df_with_missing.to_hdf("file.h5", key="df_with_missing", format="table", mode="w")
42474247
42484248
pd.read_hdf("file.h5", "df_with_missing")
42494249
42504250
df_with_missing.to_hdf(
4251-
"file.h5", "df_with_missing", format="table", mode="w", dropna=True
4251+
"file.h5", key="df_with_missing", format="table", mode="w", dropna=True
42524252
)
42534253
pd.read_hdf("file.h5", "df_with_missing")
42544254
@@ -4278,7 +4278,7 @@ This format is specified by default when using ``put`` or ``to_hdf`` or by ``for
42784278
.. ipython:: python
42794279
:okexcept:
42804280
4281-
pd.DataFrame(np.random.randn(10, 2)).to_hdf("test_fixed.h5", "df")
4281+
pd.DataFrame(np.random.randn(10, 2)).to_hdf("test_fixed.h5", key="df")
42824282
pd.read_hdf("test_fixed.h5", "df", where="index>5")
42834283
42844284
.. ipython:: python
@@ -6321,23 +6321,23 @@ The following test functions will be used below to compare the performance of se
63216321
63226322
63236323
def test_hdf_fixed_write(df):
6324-
df.to_hdf("test_fixed.hdf", "test", mode="w")
6324+
df.to_hdf("test_fixed.hdf", key="test", mode="w")
63256325
63266326
63276327
def test_hdf_fixed_read():
63286328
pd.read_hdf("test_fixed.hdf", "test")
63296329
63306330
63316331
def test_hdf_fixed_write_compress(df):
6332-
df.to_hdf("test_fixed_compress.hdf", "test", mode="w", complib="blosc")
6332+
df.to_hdf("test_fixed_compress.hdf", key="test", mode="w", complib="blosc")
63336333
63346334
63356335
def test_hdf_fixed_read_compress():
63366336
pd.read_hdf("test_fixed_compress.hdf", "test")
63376337
63386338
63396339
def test_hdf_table_write(df):
6340-
df.to_hdf("test_table.hdf", "test", mode="w", format="table")
6340+
df.to_hdf("test_table.hdf", key="test", mode="w", format="table")
63416341
63426342
63436343
def test_hdf_table_read():
@@ -6346,7 +6346,7 @@ The following test functions will be used below to compare the performance of se
63466346
63476347
def test_hdf_table_write_compress(df):
63486348
df.to_hdf(
6349-
"test_table_compress.hdf", "test", mode="w", complib="blosc", format="table"
6349+
"test_table_compress.hdf", key="test", mode="w", complib="blosc", format="table"
63506350
)
63516351
63526352

doc/source/whatsnew/v0.11.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ Enhancements
347347
.. ipython:: python
348348
349349
df = pd.DataFrame({'A': range(5), 'B': range(5)})
350-
df.to_hdf('store.h5', 'table', append=True)
350+
df.to_hdf('store.h5', key='table', append=True)
351351
pd.read_hdf('store.h5', 'table', where=['index > 2'])
352352
353353
.. ipython:: python

doc/source/whatsnew/v0.13.0.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ HDFStore API changes
385385
dfq = pd.DataFrame(np.random.randn(10, 4),
386386
columns=list('ABCD'),
387387
index=pd.date_range('20130101', periods=10))
388-
dfq.to_hdf(path, 'dfq', format='table', data_columns=True)
388+
dfq.to_hdf(path, key='dfq', format='table', data_columns=True)
389389
390390
Use boolean expressions, with in-line function evaluation.
391391

@@ -415,9 +415,9 @@ HDFStore API changes
415415
416416
path = 'test.h5'
417417
df = pd.DataFrame(np.random.randn(10, 2))
418-
df.to_hdf(path, 'df_table', format='table')
419-
df.to_hdf(path, 'df_table2', append=True)
420-
df.to_hdf(path, 'df_fixed')
418+
df.to_hdf(path, key='df_table', format='table')
419+
df.to_hdf(path, key='df_table2', append=True)
420+
df.to_hdf(path, key='df_fixed')
421421
with pd.HDFStore(path) as store:
422422
print(store)
423423

doc/source/whatsnew/v0.17.0.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ Previous behavior:
793793
794794
In [27]:
795795
df_with_missing.to_hdf('file.h5',
796-
'df_with_missing',
796+
key='df_with_missing',
797797
format='table',
798798
mode='w')
799799
@@ -809,7 +809,7 @@ New behavior:
809809

810810
.. ipython:: python
811811
812-
df_with_missing.to_hdf("file.h5", "df_with_missing", format="table", mode="w")
812+
df_with_missing.to_hdf("file.h5", key="df_with_missing", format="table", mode="w")
813813
814814
pd.read_hdf("file.h5", "df_with_missing")
815815

doc/source/whatsnew/v0.20.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ usually resulting in an invalid comparison, returning an empty result frame. The
10591059
.. ipython:: python
10601060
10611061
df = pd.DataFrame({'unparsed_date': ['2014-01-01', '2014-01-01']})
1062-
df.to_hdf('store.h5', 'key', format='table', data_columns=True)
1062+
df.to_hdf('store.h5', key='key', format='table', data_columns=True)
10631063
df.dtypes
10641064
10651065
Previous behavior:

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Other API changes
9292

9393
Deprecations
9494
~~~~~~~~~~~~
95+
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_hdf` except ``path_or_buf``. (:issue:`54229`)
9596
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_pickle` except ``path``. (:issue:`54229`)
9697
-
9798

pandas/core/generic.py

+3
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,9 @@ def to_json(
26422642
)
26432643

26442644
@final
2645+
@deprecate_nonkeyword_arguments(
2646+
version="3.0", allowed_args=["self", "path_or_buf"], name="to_hdf"
2647+
)
26452648
def to_hdf(
26462649
self,
26472650
path_or_buf: FilePath | HDFStore,

pandas/tests/io/pytables/test_append.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ def test_append_hierarchical(tmp_path, setup_path, multiindex_dataframe_random_d
643643
tm.assert_frame_equal(result, expected)
644644

645645
path = tmp_path / "test.hdf"
646-
df.to_hdf(path, "df", format="table")
646+
df.to_hdf(path, key="df", format="table")
647647
result = read_hdf(path, "df", columns=["A", "B"])
648648
expected = df.reindex(columns=["A", "B"])
649649
tm.assert_frame_equal(result, expected)

pandas/tests/io/pytables/test_categorical.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def test_categorical_conversion(tmp_path, setup_path):
152152
# We are expecting an empty DataFrame matching types of df
153153
expected = df.iloc[[], :]
154154
path = tmp_path / setup_path
155-
df.to_hdf(path, "df", format="table", data_columns=True)
155+
df.to_hdf(path, key="df", format="table", data_columns=True)
156156
result = read_hdf(path, "df", where="obsids=B")
157157
tm.assert_frame_equal(result, expected)
158158

@@ -163,7 +163,7 @@ def test_categorical_conversion(tmp_path, setup_path):
163163
# We are expecting an empty DataFrame matching types of df
164164
expected = df.iloc[[], :]
165165
path = tmp_path / setup_path
166-
df.to_hdf(path, "df", format="table", data_columns=True)
166+
df.to_hdf(path, key="df", format="table", data_columns=True)
167167
result = read_hdf(path, "df", where="obsids=B")
168168
tm.assert_frame_equal(result, expected)
169169

@@ -185,7 +185,7 @@ def test_categorical_nan_only_columns(tmp_path, setup_path):
185185
df["d"] = df.b.astype("category")
186186
expected = df
187187
path = tmp_path / setup_path
188-
df.to_hdf(path, "df", format="table", data_columns=True)
188+
df.to_hdf(path, key="df", format="table", data_columns=True)
189189
result = read_hdf(path, "df")
190190
tm.assert_frame_equal(result, expected)
191191

@@ -209,6 +209,6 @@ def test_convert_value(
209209
expected.col = expected.col.cat.set_categories(categorical_values)
210210

211211
path = tmp_path / setup_path
212-
df.to_hdf(path, "df", format="table", min_itemsize=max_widths)
212+
df.to_hdf(path, key="df", format="table", min_itemsize=max_widths)
213213
result = read_hdf(path, where=where)
214214
tm.assert_frame_equal(result, expected)

pandas/tests/io/pytables/test_complex.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_complex_fixed(tmp_path, setup_path):
2020
)
2121

2222
path = tmp_path / setup_path
23-
df.to_hdf(path, "df")
23+
df.to_hdf(path, key="df")
2424
reread = read_hdf(path, "df")
2525
tm.assert_frame_equal(df, reread)
2626

@@ -30,7 +30,7 @@ def test_complex_fixed(tmp_path, setup_path):
3030
columns=list("ABCDE"),
3131
)
3232
path = tmp_path / setup_path
33-
df.to_hdf(path, "df")
33+
df.to_hdf(path, key="df")
3434
reread = read_hdf(path, "df")
3535
tm.assert_frame_equal(df, reread)
3636

@@ -43,8 +43,8 @@ def test_complex_table(tmp_path, setup_path):
4343
)
4444

4545
path = tmp_path / setup_path
46-
df.to_hdf(path, "df", format="table")
47-
reread = read_hdf(path, "df")
46+
df.to_hdf(path, key="df", format="table")
47+
reread = read_hdf(path, key="df")
4848
tm.assert_frame_equal(df, reread)
4949

5050
df = DataFrame(
@@ -54,7 +54,7 @@ def test_complex_table(tmp_path, setup_path):
5454
)
5555

5656
path = tmp_path / setup_path
57-
df.to_hdf(path, "df", format="table", mode="w")
57+
df.to_hdf(path, key="df", format="table", mode="w")
5858
reread = read_hdf(path, "df")
5959
tm.assert_frame_equal(df, reread)
6060

@@ -77,7 +77,7 @@ def test_complex_mixed_fixed(tmp_path, setup_path):
7777
index=list("abcd"),
7878
)
7979
path = tmp_path / setup_path
80-
df.to_hdf(path, "df")
80+
df.to_hdf(path, key="df")
8181
reread = read_hdf(path, "df")
8282
tm.assert_frame_equal(df, reread)
8383

@@ -106,7 +106,7 @@ def test_complex_mixed_table(tmp_path, setup_path):
106106
tm.assert_frame_equal(df.loc[df.A > 2], result)
107107

108108
path = tmp_path / setup_path
109-
df.to_hdf(path, "df", format="table")
109+
df.to_hdf(path, key="df", format="table")
110110
reread = read_hdf(path, "df")
111111
tm.assert_frame_equal(df, reread)
112112

@@ -120,7 +120,7 @@ def test_complex_across_dimensions_fixed(tmp_path, setup_path):
120120
comps = [tm.assert_series_equal, tm.assert_frame_equal]
121121
for obj, comp in zip(objs, comps):
122122
path = tmp_path / setup_path
123-
obj.to_hdf(path, "obj", format="fixed")
123+
obj.to_hdf(path, key="obj", format="fixed")
124124
reread = read_hdf(path, "obj")
125125
comp(obj, reread)
126126

@@ -131,7 +131,7 @@ def test_complex_across_dimensions(tmp_path, setup_path):
131131
df = DataFrame({"A": s, "B": s})
132132

133133
path = tmp_path / setup_path
134-
df.to_hdf(path, "obj", format="table")
134+
df.to_hdf(path, key="obj", format="table")
135135
reread = read_hdf(path, "obj")
136136
tm.assert_frame_equal(df, reread)
137137

@@ -172,10 +172,10 @@ def test_complex_series_error(tmp_path, setup_path):
172172

173173
path = tmp_path / setup_path
174174
with pytest.raises(TypeError, match=msg):
175-
s.to_hdf(path, "obj", format="t")
175+
s.to_hdf(path, key="obj", format="t")
176176

177177
path = tmp_path / setup_path
178-
s.to_hdf(path, "obj", format="t", index=False)
178+
s.to_hdf(path, key="obj", format="t", index=False)
179179
reread = read_hdf(path, "obj")
180180
tm.assert_series_equal(s, reread)
181181

pandas/tests/io/pytables/test_errors.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_invalid_terms(tmp_path, setup_path):
115115
columns=list("ABCD"),
116116
index=date_range("20130101", periods=10),
117117
)
118-
dfq.to_hdf(path, "dfq", format="table", data_columns=True)
118+
dfq.to_hdf(path, key="dfq", format="table", data_columns=True)
119119

120120
# check ok
121121
read_hdf(path, "dfq", where="index>Timestamp('20130104') & columns=['A', 'B']")
@@ -128,7 +128,7 @@ def test_invalid_terms(tmp_path, setup_path):
128128
columns=list("ABCD"),
129129
index=date_range("20130101", periods=10),
130130
)
131-
dfq.to_hdf(path, "dfq", format="table")
131+
dfq.to_hdf(path, key="dfq", format="table")
132132

133133
msg = (
134134
r"The passed where expression: A>0 or C>0\n\s*"
@@ -169,7 +169,7 @@ def test_invalid_complib(setup_path):
169169
with tm.ensure_clean(setup_path) as path:
170170
msg = r"complib only supports \[.*\] compression."
171171
with pytest.raises(ValueError, match=msg):
172-
df.to_hdf(path, "df", complib="foolib")
172+
df.to_hdf(path, key="df", complib="foolib")
173173

174174

175175
@pytest.mark.parametrize(
@@ -185,7 +185,7 @@ def test_to_hdf_multiindex_extension_dtype(idx, tmp_path, setup_path):
185185
df = DataFrame(0, index=mi, columns=["a"])
186186
path = tmp_path / setup_path
187187
with pytest.raises(NotImplementedError, match="Saving a MultiIndex"):
188-
df.to_hdf(path, "df")
188+
df.to_hdf(path, key="df")
189189

190190

191191
def test_unsuppored_hdf_file_error(datapath):
@@ -212,7 +212,7 @@ def test_read_hdf_errors(setup_path, tmp_path):
212212
with pytest.raises(OSError, match=msg):
213213
read_hdf(path, "key")
214214

215-
df.to_hdf(path, "df")
215+
df.to_hdf(path, key="df")
216216
store = HDFStore(path, mode="r")
217217
store.close()
218218

0 commit comments

Comments
 (0)