Skip to content

HDFStore append_to_multiple with min_itemsize #34939

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
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,7 @@ I/O
- :meth:`HDFStore.keys` has now an optional `include` parameter that allows the retrieval of all native HDF5 table names (:issue:`29916`)
- Bug in :meth:`read_excel` for ODS files removes 0.0 values (:issue:`27222`)
- Bug in :meth:`ujson.encode` was raising an `OverflowError` with numbers larger than sys.maxsize (:issue: `34395`)
- Bug in :meth:`HDFStore.append_to_multiple` was raising a ``ValueError`` when the min_itemsize parameter is set (:issue:`11238`)

Plotting
^^^^^^^^
Expand Down
9 changes: 8 additions & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,14 +1303,21 @@ def append_to_multiple(
valid_index = valid_index.intersection(index)
value = value.loc[valid_index]

min_itemsize = kwargs.pop("min_itemsize", None)

# append
for k, v in d.items():
dc = data_columns if k == selector else None

# compute the val
val = value.reindex(v, axis=axis)

self.append(k, val, data_columns=dc, **kwargs)
filtered = (
{key: value for (key, value) in min_itemsize.items() if key in v}
if min_itemsize is not None
else None
)
self.append(k, val, data_columns=dc, min_itemsize=filtered, **kwargs)

def create_table_index(
self,
Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/io/pytables/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -3697,6 +3697,33 @@ def test_append_to_multiple_dropna_false(self, setup_path):

assert not store.select("df1a").index.equals(store.select("df2a").index)

def test_append_to_multiple_min_itemsize(self, setup_path):
# GH 11238
df = pd.DataFrame(
{
"IX": np.arange(1, 21),
"Num": np.arange(1, 21),
"BigNum": np.arange(1, 21) * 88,
"Str": ["a" for _ in range(20)],
"LongStr": ["abcde" for _ in range(20)],
}
)
expected = df.iloc[[0]]

with ensure_clean_store(setup_path) as store:
store.append_to_multiple(
{
"index": ["IX"],
"nums": ["Num", "BigNum"],
"strs": ["Str", "LongStr"],
},
df.iloc[[0]],
"index",
min_itemsize={"Str": 10, "LongStr": 100, "Num": 2},
)
result = store.select_as_multiple(["index", "nums", "strs"])
tm.assert_frame_equal(result, expected)

def test_select_as_multiple(self, setup_path):

df1 = tm.makeTimeDataFrame()
Expand Down