You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeError: cannot do slice indexing on <class'pandas.tseries.index.DatetimeIndex'>with these indexers [2] of <type'int'>
311
+
312
+
.. code-block:: python
313
+
314
+
In [8]: sl.loc[-1.0:2]
315
+
TypeError: cannot do slice indexing on <class'pandas.core.index.Int64Index'>with these indexers [-1.0] of <type'float'>
316
+
317
+
318
+
String likes in slicing *can* be convertible to the type of the index and lead to natural slicing.
319
+
320
+
.. ipython:: python
321
+
322
+
dfl.loc['20130102':'20130104']
323
+
295
324
pandas provides a suite of methods in order to have **purely label based indexing**. This is a strict inclusion based protocol.
296
325
**at least 1** of the labels for which you ask, must be in the index or a ``KeyError`` will be raised! When slicing, the start bound is *included*, **AND** the stop bound is *included*. Integers are valid labels, but they refer to the label **and not the position**.
297
326
@@ -1486,5 +1515,3 @@ This will **not** work at all, and so should be avoided
1486
1515
The chained assignment warnings / exceptions are aiming to inform the user of a possibly invalid
1487
1516
assignment. There may be false positives; situations where a chained assignment is inadvertantly
Copy file name to clipboardExpand all lines: doc/source/whatsnew/v0.16.0.txt
+60
Original file line number
Diff line number
Diff line change
@@ -211,6 +211,66 @@ Backwards incompatible API changes
211
211
p // 0
212
212
213
213
214
+
Indexing Changes
215
+
~~~~~~~~~~~~~~~~
216
+
217
+
.. _whatsnew_0160.api_breaking.indexing:
218
+
219
+
The behavior of a small sub-set of edge cases for using ``.loc`` have changed (:issue:`8613`). Furthermore we have improved the content of the error messages that are raised:
220
+
221
+
- slicing with ``.loc`` where the start and/or stop bound is not found in the index is now allowed; this previously would raise a ``KeyError``. This makes the behavior the same as ``.ix`` in this case. This change is only for slicing, not when indexing with a single label.
KeyError: 'stop bound [2013-01-10] is not in the [index]'
236
+
237
+
In [6]: s.loc[-10:3]
238
+
KeyError: 'start bound [-10] is not the [index]'
239
+
240
+
In [8]: s.loc[-1.0:2]
241
+
Out[2]:
242
+
-1 1
243
+
1 2
244
+
2 3
245
+
dtype: int64
246
+
247
+
New Behavior
248
+
249
+
.. ipython:: python
250
+
251
+
df.loc['2013-01-02':'2013-01-10']
252
+
s.loc[-10:3]
253
+
254
+
.. code-block:: python
255
+
256
+
In [8]: s.loc[-1.0:2]
257
+
TypeError: cannot do slice indexing on <class 'pandas.core.index.Int64Index'> with these indexers [-1.0] of <type 'float'>
258
+
259
+
- provide a useful exception for indexing with an invalid type for that index when using ``.loc``. For example trying to use ``.loc`` on an index of type ``DatetimeIndex`` or ``PeriodIndex`` or ``TimedeltaIndex``, with an integer (or a float).
260
+
261
+
Previous Behavior
262
+
263
+
.. code-block:: python
264
+
265
+
In [4]: df.loc[2:3]
266
+
KeyError: 'start bound [2] is not the [index]'
267
+
268
+
New Behavior
269
+
270
+
.. code-block:: python
271
+
272
+
In [4]: df.loc[2:3]
273
+
TypeError: Cannot do slice indexing on <class 'pandas.tseries.index.DatetimeIndex'> with <type 'int'> keys
0 commit comments