Skip to content

Commit 07d673f

Browse files
committed
API: indexing with a null key will raise a TypeError rather than a ValueError, #11356
1 parent 56d545c commit 07d673f

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

doc/source/whatsnew/v0.17.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ API changes
4141
- Regression from 0.16.2 for output formatting of long floats/nan, restored in (:issue:`11302`)
4242
- Prettyprinting sets (e.g. in DataFrame cells) now uses set literal syntax (``{x, y}``) instead of
4343
Legacy Python syntax (``set([x, y])``) (:issue:`11215`)
44+
- Indexing with a null key will raise a ``TypeError``, instead of a ``ValueError`` (:issue:`11356`)
4445

4546
.. _whatsnew_0171.deprecations:
4647

pandas/core/index.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -862,9 +862,10 @@ def to_int():
862862
return self._invalid_indexer('label', key)
863863

864864
if is_float(key):
865-
if not self.is_floating():
866-
warnings.warn("scalar indexers for index type {0} should be integers and not floating point".format(
867-
type(self).__name__), FutureWarning, stacklevel=3)
865+
if isnull(key):
866+
return self._invalid_indexer('label', key)
867+
warnings.warn("scalar indexers for index type {0} should be integers and not floating point".format(
868+
type(self).__name__), FutureWarning, stacklevel=3)
868869
return to_int()
869870

870871
return key
@@ -3721,9 +3722,23 @@ def astype(self, dtype):
37213722
return Index(self._values, name=self.name, dtype=dtype)
37223723

37233724
def _convert_scalar_indexer(self, key, kind=None):
3725+
"""
3726+
convert a scalar indexer
3727+
3728+
Parameters
3729+
----------
3730+
key : label of the slice bound
3731+
kind : optional, type of the indexing operation (loc/ix/iloc/None)
3732+
3733+
right now we are converting
3734+
floats -> ints if the index supports it
3735+
"""
3736+
37243737
if kind == 'iloc':
3725-
return super(Float64Index, self)._convert_scalar_indexer(key,
3726-
kind=kind)
3738+
if is_integer(key):
3739+
return key
3740+
return super(Float64Index, self)._convert_scalar_indexer(key, kind=kind)
3741+
37273742
return key
37283743

37293744
def _convert_slice_indexer(self, key, kind=None):

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@ def _has_valid_type(self, key, axis):
12851285

12861286
def error():
12871287
if isnull(key):
1288-
raise ValueError(
1288+
raise TypeError(
12891289
"cannot use label indexing with a null key")
12901290
raise KeyError("the label [%s] is not in the [%s]" %
12911291
(key, self.obj._get_axis_name(axis)))

pandas/core/internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3217,7 +3217,7 @@ def get(self, item, fastpath=True):
32173217
else:
32183218

32193219
if isnull(item):
3220-
raise ValueError("cannot label index with a null key")
3220+
raise TypeError("cannot label index with a null key")
32213221

32223222
indexer = self.items.get_indexer_for([item])
32233223
return self.reindex_indexer(new_axis=self.items[indexer],

pandas/tests/test_frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5839,7 +5839,7 @@ def check(df):
58395839

58405840
def f():
58415841
df.loc[:,np.nan]
5842-
self.assertRaises(ValueError, f)
5842+
self.assertRaises(TypeError, f)
58435843

58445844

58455845
df = DataFrame([[1,2,3],[4,5,6]], index=[1,np.nan])

0 commit comments

Comments
 (0)