Skip to content

Commit 063ff4b

Browse files
committed
ENH: also address Series setitem per #328
1 parent 76c16e9 commit 063ff4b

File tree

7 files changed

+39
-38
lines changed

7 files changed

+39
-38
lines changed

pandas/core/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,11 @@ def _is_bool_indexer(key):
324324
return False
325325

326326
def _default_index(n):
327-
from pandas.core.index import NULL_INDEX
327+
from pandas.core.index import NULL_INDEX, Index
328328
if n == 0:
329329
return NULL_INDEX
330330
else:
331-
return np.arange(n)
331+
return Index(np.arange(n))
332332

333333
def ensure_float(arr):
334334
if issubclass(arr.dtype.type, np.integer):

pandas/core/indexing.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ class IndexingError(Exception):
1414
pass
1515

1616

17-
class AmbiguousIndexError(Exception):
18-
pass
19-
20-
2117
class _NDFrameIndexer(object):
2218

2319
def __init__(self, obj):
@@ -391,11 +387,7 @@ def _is_integer_dtype(arr):
391387
return issubclass(arr.dtype.type, np.integer)
392388

393389
def _is_integer_index(index):
394-
# make an educated and not too intelligent guess
395-
if len(index) == 0: # pragma: no cover
396-
return False
397-
else:
398-
return com.is_integer(index[0])
390+
return index.inferred_type == 'integer'
399391

400392
def _is_label_like(key):
401393
# select a label or row

pandas/core/series.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,17 @@ def __new__(cls, data=None, index=None, dtype=None, name=None,
161161

162162
if index is None:
163163
index = _default_index(len(subarr))
164+
else:
165+
index = _ensure_index(index)
164166

165167
# Change the class of the array to be the subclass type.
166-
subarr = subarr.view(cls)
168+
if index.is_all_dates:
169+
subarr = subarr.view(TimeSeries)
170+
else:
171+
subarr = subarr.view(Series)
167172
subarr.index = index
168173
subarr.name = name
169174

170-
if subarr.index.is_all_dates:
171-
subarr = subarr.view(TimeSeries)
172-
173175
return subarr
174176

175177
def __init__(self, data=None, index=None, dtype=None, name=None,
@@ -288,7 +290,7 @@ def __getitem__(self, key):
288290
pass
289291

290292
if index.inferred_type == 'integer':
291-
raise AmbiguousIndexError(key)
293+
raise # AmbiguousIndexError(key)
292294

293295
try:
294296
return _gin.get_value_at(self, key)
@@ -402,9 +404,12 @@ def __setitem__(self, key, value):
402404
values[self.index.get_loc(key)] = value
403405
return
404406
except KeyError:
405-
if isinstance(key, (int, np.integer)):
407+
if (com.is_integer(key)
408+
and not self.index.inferred_type == 'integer'):
409+
406410
values[key] = value
407411
return
412+
408413
raise KeyError('%s not in this series!' % str(key))
409414
except TypeError:
410415
# Could not hash item
@@ -597,6 +602,7 @@ def keys(self):
597602
"Alias for index"
598603
return self.index
599604

605+
# alas, I wish this worked
600606
# values = lib.ValuesProperty()
601607

602608
@property

pandas/src/inference.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ def is_bool_array(ndarray values):
9696
else:
9797
return False
9898

99+
def is_integer(object o):
100+
return util.is_integer_object(o)
101+
99102
def is_integer_array(ndarray values):
100103
cdef:
101104
Py_ssize_t i, n = len(values)

pandas/src/numpy_helper.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@ get_value_1d(PyArrayObject* ap, Py_ssize_t i) {
7272
return PyArray_Scalar(item, PyArray_DESCR(ap), (PyObject*) ap);
7373
}
7474

75-
PANDAS_INLINE PyObject*
76-
get_base_ndarray(PyObject* ap) {
77-
// if (!ap || (NULL == ap)) {
78-
// Py_RETURN_NONE;
79-
// }
75+
// PANDAS_INLINE PyObject*
76+
// get_base_ndarray(PyObject* ap) {
77+
// // if (!ap || (NULL == ap)) {
78+
// // Py_RETURN_NONE;
79+
// // }
8080

81-
while (!PyArray_CheckExact(ap)) {
82-
ap = PyArray_BASE((PyArrayObject*) ap);
83-
if (ap == Py_None) Py_RETURN_NONE;
84-
}
85-
// PyArray_BASE is a borrowed reference
86-
if(ap) {
87-
Py_INCREF(ap);
88-
}
89-
return ap;
90-
}
81+
// while (!PyArray_CheckExact(ap)) {
82+
// ap = PyArray_BASE((PyArrayObject*) ap);
83+
// if (ap == Py_None) Py_RETURN_NONE;
84+
// }
85+
// // PyArray_BASE is a borrowed reference
86+
// if(ap) {
87+
// Py_INCREF(ap);
88+
// }
89+
// return ap;
90+
// }

pandas/src/properties.pyx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,8 @@ cdef class ValuesProperty(object):
6666
object base
6767

6868
base = np.get_array_base(arr)
69-
if base is None:
69+
if base is None or not np.PyArray_CheckExact(base):
7070
arr = arr.view(np.ndarray)
7171
else:
7272
arr = base
73-
while arr is not None and not np.PyArray_CheckExact(arr):
74-
base = np.get_array_base(arr)
75-
if base is None:
76-
break
77-
arr = base
7873
return arr

pandas/tests/test_series.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ def test_getitem_ambiguous_keyerror(self):
405405
self.assertRaises(KeyError, s.__getitem__, 1)
406406
self.assertRaises(KeyError, s.ix.__getitem__, 1)
407407

408+
def test_setitem_ambiguous_keyerror(self):
409+
s = Series(range(10), index=range(0, 20, 2))
410+
self.assertRaises(KeyError, s.__setitem__, 1, 5)
411+
self.assertRaises(KeyError, s.ix.__setitem__, 1, 5)
412+
408413
def test_slice(self):
409414
numSlice = self.series[10:20]
410415
numSliceEnd = self.series[-10:]

0 commit comments

Comments
 (0)