Skip to content

Commit b358876

Browse files
committed
DOC: add addtl docs for FloatIndexing changes in 0.18.0
closes #12322 Author: Jeff Reback <[email protected]> Closes #12330 from jreback/deprecate_docs and squashes the following commits: 5a9585c [Jeff Reback] DOC: add addtl docs for FloatIndexing changes in 0.18.0
1 parent 8f1a318 commit b358876

File tree

4 files changed

+69
-13
lines changed

4 files changed

+69
-13
lines changed

doc/source/advanced.rst

+15-4
Original file line numberDiff line numberDiff line change
@@ -795,12 +795,23 @@ In non-float indexes, slicing using floats will raise a ``TypeError``
795795
In [1]: pd.Series(range(5))[3.5:4.5]
796796
TypeError: the slice start [3.5] is not a proper indexer for this index type (Int64Index)
797797
798-
Using a scalar float indexer will be deprecated in a future version, but is allowed for now.
798+
.. warning::
799799
800-
.. code-block:: python
800+
Using a scalar float indexer has been removed in 0.18.0, so the following will raise a ``TypeError``
801+
802+
.. code-block:: python
803+
804+
In [3]: pd.Series(range(5))[3.0]
805+
TypeError: cannot do label indexing on <class 'pandas.indexes.range.RangeIndex'> with these indexers [3.0] of <type 'float'>
806+
807+
Further the treatment of ``.ix`` with a float indexer on a non-float index, will be label based, and thus coerce the index.
808+
809+
.. ipython:: python
801810
802-
In [3]: pd.Series(range(5))[3.0]
803-
Out[3]: 3
811+
s2 = pd.Series([1, 2, 3], index=list('abc'))
812+
s2
813+
s2.ix[1.0] = 10
814+
s2
804815
805816
Here is a typical use-case for using this type of indexing. Imagine that you have a somewhat
806817
irregular timedelta-like indexing scheme, but the data is recorded as floats. This could for

doc/source/whatsnew/v0.18.0.txt

+36-8
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,8 @@ performed with the ``Resampler`` objects with :meth:`~Resampler.backfill`,
704704

705705
.. ipython:: python
706706

707-
s = Series(np.arange(5,dtype='int64'),
708-
index=date_range('2010-01-01', periods=5, freq='Q'))
707+
s = pd.Series(np.arange(5,dtype='int64'),
708+
index=date_range('2010-01-01', periods=5, freq='Q'))
709709
s
710710

711711
Previously
@@ -838,7 +838,7 @@ Deprecations
838838

839839
.. code-block:: python
840840

841-
In [1]: s = Series(range(3))
841+
In [1]: s = pd.Series(range(3))
842842

843843
In [2]: pd.rolling_mean(s,window=2,min_periods=1)
844844
FutureWarning: pd.rolling_mean is deprecated for Series and
@@ -884,11 +884,17 @@ Removal of deprecated float indexers
884884
In :issue:`4892` indexing with floating point numbers on a non-``Float64Index`` was deprecated (in version 0.14.0).
885885
In 0.18.0, this deprecation warning is removed and these will now raise a ``TypeError``. (:issue:`12165`)
886886

887+
.. ipython:: python
888+
889+
s = pd.Series([1,2,3])
890+
s
891+
s2 = pd.Series([1, 2, 3], index=list('abc'))
892+
s2
893+
887894
Previous Behavior:
888895

889896
.. code-block:: python
890897

891-
In [1]: s = Series([1,2,3])
892898
In [2]: s[1.0]
893899
FutureWarning: scalar indexers for index type Int64Index should be integers and not floating point
894900
Out[2]: 2
@@ -901,24 +907,46 @@ Previous Behavior:
901907
FutureWarning: scalar indexers for index type Int64Index should be integers and not floating point
902908
Out[4]: 2
903909

910+
# .ix would coerce 1.0 to the positional 1, and index
911+
In [5]: s2.ix[1.0] = 10
912+
FutureWarning: scalar indexers for index type Index should be integers and not floating point
913+
914+
In [6]: s2
915+
Out[6]:
916+
a 1
917+
b 10
918+
c 3
919+
dtype: int64
920+
904921
New Behavior:
905922

906923
.. code-block:: python
907924

908-
In [4]: s[1.0]
925+
In [2]: s[1.0]
909926
TypeError: cannot do label indexing on <class 'pandas.indexes.range.RangeIndex'> with these indexers [1.0] of <type 'float'>
910927

911-
In [4]: s.iloc[1.0]
928+
In [3]: s.iloc[1.0]
912929
TypeError: cannot do label indexing on <class 'pandas.indexes.range.RangeIndex'> with these indexers [1.0] of <type 'float'>
913930

914931
In [4]: s.loc[1.0]
915932
TypeError: cannot do label indexing on <class 'pandas.indexes.range.RangeIndex'> with these indexers [1.0] of <type 'float'>
916933

934+
# .ix will now cause this to be a label lookup and coerce to and Index
935+
In [5]: s2.ix[1.0] = 10
936+
937+
In [6]: s2
938+
Out[3]:
939+
a 1
940+
b 2
941+
c 3
942+
1.0 10
943+
dtype: int64
944+
917945
Float indexing on a ``Float64Index`` is unchanged.
918946

919947
.. ipython:: python
920948

921-
s = Series([1,2,3],index=np.arange(3.))
949+
s = pd.Series([1,2,3],index=np.arange(3.))
922950
s[1.0]
923951
s[1.0:2.5]
924952

@@ -945,7 +973,7 @@ Performance Improvements
945973
- Improved huge ``DatetimeIndex``, ``PeriodIndex`` and ``TimedeltaIndex``'s ops performance including ``NaT`` (:issue:`10277`)
946974
- Improved performance of ``pandas.concat`` (:issue:`11958`)
947975
- Improved performance of ``StataReader`` (:issue:`11591`)
948-
- Improved performance in construction of ``Categoricals`` with Series of datetimes containing ``NaT`` (:issue:`12077`)
976+
- Improved performance in construction of ``Categoricals`` with ``Series`` of datetimes containing ``NaT`` (:issue:`12077`)
949977

950978

951979
- Improved performance of ISO 8601 date parsing for dates without separators (:issue:`11899`), leading zeros (:issue:`11871`) and with whitespace preceding the time zone (:issue:`9714`)

pandas/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ def _convert_scalar_indexer(self, key, kind=None):
974974
if kind == 'iloc':
975975
if is_integer(key):
976976
return key
977-
return self._invalid_indexer('label', key)
977+
return self._invalid_indexer('positional', key)
978978
else:
979979

980980
if len(self):

pandas/tests/test_indexing.py

+17
Original file line numberDiff line numberDiff line change
@@ -5406,6 +5406,23 @@ def test_index_type_coercion(self):
54065406
s2['0'] = 0
54075407
self.assertTrue(s2.index.is_object())
54085408

5409+
def test_invalid_scalar_float_indexers_error(self):
5410+
5411+
for index in [tm.makeStringIndex, tm.makeUnicodeIndex,
5412+
tm.makeCategoricalIndex,
5413+
tm.makeDateIndex, tm.makeTimedeltaIndex,
5414+
tm.makePeriodIndex]:
5415+
5416+
i = index(5)
5417+
5418+
s = Series(np.arange(len(i)), index=i)
5419+
5420+
def f():
5421+
s.iloc[3.0]
5422+
self.assertRaisesRegexp(TypeError,
5423+
'cannot do positional indexing',
5424+
f)
5425+
54095426
def test_invalid_scalar_float_indexers(self):
54105427

54115428
# GH 4892

0 commit comments

Comments
 (0)