Skip to content

Commit b9ccc53

Browse files
committed
DOC: Sub-section Query MultiIndex if IO user guide
Document the use of level name for MultiIndex in HDFStore.
1 parent 76516b4 commit b9ccc53

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

doc/source/user_guide/io.rst

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3811,6 +3811,8 @@ storing/selecting from homogeneous index ``DataFrames``.
38113811
# the levels are automatically included as data columns
38123812
store.select('df_mi', 'foo=bar')
38133813
3814+
.. note::
3815+
The ``index`` keyword is reserved and cannot be use as a level name.
38143816

38153817
.. _io.hdf5-query:
38163818

@@ -3934,29 +3936,6 @@ returned, this is equivalent to passing a
39343936
39353937
store.select('df', "columns=['A', 'B']")
39363938
3937-
Using level name when querying MultiIndex DataFrame.
3938-
3939-
.. ipython:: python
3940-
3941-
store.select('df_mi', "foo=baz and bar=two")
3942-
3943-
Using ``level_0`` and ``level_1`` for MultiIndex without name.
3944-
3945-
.. ipython:: python
3946-
3947-
index = pd.MultiIndex(
3948-
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
3949-
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
3950-
)
3951-
df_mi_2 = pd.DataFrame(np.random.randn(10, 3),
3952-
index=index, columns=["A", "B", "C"])
3953-
df_mi_2
3954-
3955-
store.append("df_mi_2", df_mi_2)
3956-
3957-
# the levels are automatically included as data columns
3958-
store.select("df_mi_2", "level_0=foo and level_1=two")
3959-
39603939
``start`` and ``stop`` parameters can be specified to limit the total search
39613940
space. These are in terms of the total number of rows in a table.
39623941

@@ -3971,7 +3950,7 @@ space. These are in terms of the total number of rows in a table.
39713950

39723951
.. _io.hdf5-timedelta:
39733952

3974-
Using timedelta64[ns]
3953+
Query timedelta64[ns]
39753954
+++++++++++++++++++++
39763955

39773956
You can store and query using the ``timedelta64[ns]`` type. Terms can be
@@ -3990,6 +3969,35 @@ specified in the format: ``<float>(<unit>)``, where float may be signed (and fra
39903969
store.append('dftd', dftd, data_columns=True)
39913970
store.select('dftd', "C<'-3.5D'")
39923971
3972+
Query MultiIndex
3973+
++++++++++++++++
3974+
3975+
Selecting from a ``MultiIndex`` can be achieved by using the name of the level.
3976+
3977+
.. ipython:: python
3978+
3979+
df_mi.index.names
3980+
store.select('df_mi', "foo=baz and bar=two")
3981+
3982+
If the ``MultiIndex`` levels names are ``None``, the levels are automatically made available via
3983+
the ``level_n`` keyword with ``n`` the level of the ``MultiIndex`` you want to select from.
3984+
3985+
.. ipython:: python
3986+
3987+
index = pd.MultiIndex(
3988+
levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]],
3989+
codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
3990+
)
3991+
df_mi_2 = pd.DataFrame(np.random.randn(10, 3),
3992+
index=index, columns=["A", "B", "C"])
3993+
df_mi_2
3994+
3995+
store.append("df_mi_2", df_mi_2)
3996+
3997+
# the levels are automatically included as data columns with keyword level_n
3998+
store.select("df_mi_2", "level_0=foo and level_1=two")
3999+
4000+
39934001
Indexing
39944002
++++++++
39954003

0 commit comments

Comments
 (0)