Skip to content

Commit f2fa98a

Browse files
committed
DOC: add example and warning on fallback indexing for float indexes.
1 parent 6790d7a commit f2fa98a

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

doc/source/indexing.rst

+40-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,8 @@ Advanced Indexing with ``.ix``
675675
explicit about indexing choices. ``.ix`` allows a great flexibility to specify
676676
indexing locations by *label* and/or *integer position*. Pandas will attempt
677677
to use any passed *integer* as *label* locations first (like what ``.loc``
678-
would do, then to fall back on *positional* indexing, like what ``.iloc`` would do).
678+
would do, then to fall back on *positional* indexing, like what ``.iloc``
679+
would do). See :ref:`Fallback Indexing <indexing.fallback>` for an example.
679680

680681
The syntax of using ``.ix`` is identical to ``.loc``, in :ref:`Selection by Label <indexing.label>`,
681682
and ``.iloc`` in :ref:`Selection by Position <indexing.integer>`.
@@ -800,6 +801,44 @@ values, though setting arbitrary vectors is not yet supported:
800801
print df2
801802
print df2.dtypes
802803
804+
805+
Fallback indexing
806+
~~~~~~~~~~~~~~~~~~~~
807+
808+
.. _indexing.fallback:
809+
810+
Float indexes should be used only with caution. If you have a float indexed
811+
``DataFrame`` and try to select using an integer, the row that Pandas returns
812+
might not be what you expect. Pandas first attempts to use the *integer*
813+
as a *label* location, but fails to find a match (because the types
814+
are not equal). Pandas then falls back to back to positional indexing.
815+
816+
.. ipython:: python
817+
818+
df = pd.DataFrame(np.random.randn(4,4),
819+
columns=list('ABCD'), index=[1.0, 2.0, 3.0, 4.0])
820+
df
821+
df.ix[1]
822+
823+
To select the row you do expect, instead use a float label or
824+
use ``iloc``.
825+
826+
.. ipython:: python
827+
828+
df.ix[1.0]
829+
df.iloc[0]
830+
831+
Instead of using a float index, it is often better to
832+
convert to an integer index:
833+
834+
.. ipython:: python
835+
836+
df_new = df.reset_index()
837+
df_new[df_new.index == 1.0]
838+
# now you can also do "float selection"
839+
df_new[(df_new.index >= 1.0) & (df_new.index < 2)]
840+
841+
803842
.. _indexing.class:
804843

805844
Index objects

0 commit comments

Comments
 (0)