Skip to content

Commit 1dcd687

Browse files
committed
Update tests and exception message.
The exception is also raised for `Panel` but not for `Series` objects. Fixes pandas-dev#7813.
1 parent 989a51b commit 1dcd687

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

pandas/io/pytables.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -2701,9 +2701,11 @@ def write(self, obj, **kwargs):
27012701

27022702
self.attrs.ndim = data.ndim
27032703
for i, ax in enumerate(data.axes):
2704-
if i == 0:
2705-
if not ax.is_unique:
2706-
raise ValueError("Columns index has to be unique for fixed format")
2704+
if i == 0 and not ax.is_unique:
2705+
raise ValueError(
2706+
"Columns index (resp. Items axis for Panels) "
2707+
"has to be unique for fixed format"
2708+
)
27072709
self.write_index('axis%d' % i, ax)
27082710

27092711
# Supporting mixed-type DataFrame objects...nontrivial

pandas/io/tests/test_pytables.py

+29
Original file line numberDiff line numberDiff line change
@@ -4571,6 +4571,35 @@ def test_duplicate_column_name(self):
45714571
other = read_hdf(path, 'df')
45724572
tm.assert_frame_equal(df, other)
45734573

4574+
def test_non_unique_items_axis(self):
4575+
panel = Panel(
4576+
data=np.zeros((2, 2, 2)),
4577+
items=["a", "a"],
4578+
major_axis=["b", "b"],
4579+
minor_axis=["c", "c"]
4580+
)
4581+
4582+
with ensure_clean_path(self.path) as path:
4583+
self.assertRaises(
4584+
ValueError, panel.to_hdf, path, 'panel', format='fixed'
4585+
)
4586+
4587+
panel.to_hdf(path, 'panel', format='table')
4588+
other = read_hdf(path, 'panel')
4589+
tm.assert_panel_equal(panel, other)
4590+
4591+
def test_non_unique_series_index(self):
4592+
series = Series([0, 0], index=["a", "a"])
4593+
4594+
with ensure_clean_path(self.path) as path:
4595+
series.to_hdf(path, 'series', format="fixed")
4596+
other = read_hdf(path, 'series')
4597+
tm.assert_series_equal(series, other)
4598+
4599+
series.to_hdf(path, 'series', format='table')
4600+
other = read_hdf(path, 'series')
4601+
tm.assert_series_equal(series, other)
4602+
45744603

45754604
def _test_sort(obj):
45764605
if isinstance(obj, DataFrame):

0 commit comments

Comments
 (0)