Skip to content

Commit f15b176

Browse files
committed
Merge pull request #3128 from adamgreenhall/doc_fallback_index
thanks for the pr! DOC: GH2072 add example and warning on fallback indexing for float indexes.
2 parents fadfd90 + f2fa98a commit f15b176

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
@@ -677,7 +677,8 @@ Advanced Indexing with ``.ix``
677677
explicit about indexing choices. ``.ix`` allows a great flexibility to specify
678678
indexing locations by *label* and/or *integer position*. Pandas will attempt
679679
to use any passed *integer* as *label* locations first (like what ``.loc``
680-
would do, then to fall back on *positional* indexing, like what ``.iloc`` would do).
680+
would do, then to fall back on *positional* indexing, like what ``.iloc``
681+
would do). See :ref:`Fallback Indexing <indexing.fallback>` for an example.
681682

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

807846
Index objects

0 commit comments

Comments
 (0)