MCPcopy
hub / github.com/pandas-dev/pandas / test_store_multiindex

Function test_store_multiindex

pandas/tests/io/pytables/test_put.py:332–391  ·  view source on GitHub ↗
(temp_hdfstore)

Source from the content-addressed store, hash-verified

330
331
332def test_store_multiindex(temp_hdfstore):
333 # validate multi-index names
334 # GH 5527
335
336 def make_index(names=None):
337 dti = date_range("2013-12-01", "2013-12-02")
338 mi = MultiIndex.from_product([dti, range(2), range(3)], names=names)
339 return mi
340
341 # no names
342 df = DataFrame(np.zeros((12, 2)), columns=["a", "b"], index=make_index())
343 temp_hdfstore.append("df", df)
344 tm.assert_frame_equal(temp_hdfstore.select("df"), df)
345
346 # partial names
347 temp_hdfstore.remove("df")
348 df = DataFrame(
349 np.zeros((12, 2)),
350 columns=["a", "b"],
351 index=make_index(["date", None, None]),
352 )
353 temp_hdfstore.append("df", df)
354 tm.assert_frame_equal(temp_hdfstore.select("df"), df)
355
356 # series
357 ser = Series(np.zeros(12), index=make_index(["date", None, None]))
358 temp_hdfstore.append("ser", ser)
359 xp = Series(np.zeros(12), index=make_index(["date", "level_1", "level_2"]))
360 tm.assert_series_equal(temp_hdfstore.select("ser"), xp)
361
362 # dup with column
363 temp_hdfstore.remove("df")
364 df = DataFrame(
365 np.zeros((12, 2)),
366 columns=["a", "b"],
367 index=make_index(["date", "a", "t"]),
368 )
369 msg = "duplicate names/columns in the multi-index when storing as a table"
370 with pytest.raises(ValueError, match=msg):
371 temp_hdfstore.append("df", df)
372
373 # dup within level
374 temp_hdfstore.remove("df")
375 df = DataFrame(
376 np.zeros((12, 2)),
377 columns=["a", "b"],
378 index=make_index(["date", "date", "date"]),
379 )
380 with pytest.raises(ValueError, match=msg):
381 temp_hdfstore.append("df", df)
382
383 # fully names
384 temp_hdfstore.remove("df")
385 df = DataFrame(
386 np.zeros((12, 2)),
387 columns=["a", "b"],
388 index=make_index(["date", "s", "t"]),
389 )

Callers

nothing calls this directly

Calls 7

DataFrameClass · 0.90
SeriesClass · 0.90
make_indexFunction · 0.85
appendMethod · 0.45
selectMethod · 0.45
removeMethod · 0.45
raisesMethod · 0.45

Tested by

no test coverage detected