Skip to content

Commit 9b3622a

Browse files
committed
BUG: setting on a frame without an index silenty was failing, related (GH5226)
1 parent aef5061 commit 9b3622a

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

doc/source/release.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ API Changes
210210
an alias of iteritems used to get around ``2to3``'s changes).
211211
(:issue:`4384`, :issue:`4375`, :issue:`4372`)
212212
- ``Series.get`` with negative indexers now returns the same as ``[]`` (:issue:`4390`)
213-
- allow ``ix/loc`` for Series/DataFrame/Panel to set on any axis even when the single-key is not currently contained in
214-
the index for that axis (:issue:`2578`, :issue:`5226`)
213+
- allow ``ix/loc`` for Series/DataFrame/Panel to set on any axis even when the single-key
214+
is not currently contained in the index for that axis (:issue:`2578`, :issue:`5226`)
215215
- Default export for ``to_clipboard`` is now csv with a sep of `\t` for
216216
compat (:issue:`3368`)
217217
- ``at`` now will enlarge the object inplace (and return the same) (:issue:`2578`)

pandas/core/common.py

-1
Original file line numberDiff line numberDiff line change
@@ -1632,7 +1632,6 @@ def _default_index(n):
16321632
def ensure_float(arr):
16331633
if issubclass(arr.dtype.type, (np.integer, np.bool_)):
16341634
arr = arr.astype(float)
1635-
16361635
return arr
16371636

16381637

pandas/core/frame.py

+6
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,13 @@ def _ixs(self, i, axis=0, copy=False):
15731573
if isinstance(label, Index):
15741574
return self.take(i, axis=1, convert=True)
15751575

1576+
# if the values returned are not the same length
1577+
# as the index (iow a not found value), iget returns
1578+
# a 0-len ndarray. This is effectively catching
1579+
# a numpy error (as numpy should really raise)
15761580
values = self._data.iget(i)
1581+
if not len(values):
1582+
values = np.array([np.nan]*len(self.index),dtype=object)
15771583
return self._constructor_sliced.from_array(
15781584
values, index=self.index,
15791585
name=label, fastpath=True)

pandas/core/nanops.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,18 @@ def nanmin(values, axis=None, skipna=True):
300300
apply_ax = axis if axis is not None else 0
301301
result = np.apply_along_axis(builtins.min, apply_ax, values)
302302
else:
303-
result = builtins.min(values)
303+
try:
304+
result = builtins.min(values)
305+
except:
306+
result = np.nan
304307
else:
305308
if ((axis is not None and values.shape[axis] == 0)
306309
or values.size == 0):
307-
result = com.ensure_float(values.sum(axis))
308-
result.fill(np.nan)
310+
try:
311+
result = com.ensure_float(values.sum(axis))
312+
result.fill(np.nan)
313+
except:
314+
result = np.nan
309315
else:
310316
result = values.min(axis)
311317

@@ -324,12 +330,18 @@ def nanmax(values, axis=None, skipna=True):
324330
apply_ax = axis if axis is not None else 0
325331
result = np.apply_along_axis(builtins.max, apply_ax, values)
326332
else:
327-
result = builtins.max(values)
333+
try:
334+
result = builtins.max(values)
335+
except:
336+
result = np.nan
328337
else:
329338
if ((axis is not None and values.shape[axis] == 0)
330339
or values.size == 0):
331-
result = com.ensure_float(values.sum(axis))
332-
result.fill(np.nan)
340+
try:
341+
result = com.ensure_float(values.sum(axis))
342+
result.fill(np.nan)
343+
except:
344+
result = np.nan
333345
else:
334346
result = values.max(axis)
335347

pandas/tests/test_indexing.py

+20
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,26 @@ def f():
15921592
df.loc[3] = [6,7]
15931593
assert_frame_equal(df,DataFrame([[6,7]],index=[3],columns=['A','B']))
15941594

1595+
# no label overlap
1596+
df = DataFrame(columns=['A','B'])
1597+
df.loc[0] = Series(1,index=range(4))
1598+
assert_frame_equal(df,DataFrame(columns=['A','B'],index=[0]))
1599+
1600+
# no index to start
1601+
expected = DataFrame({ 0 : Series(1,index=range(4)) },columns=['A','B',0])
1602+
1603+
df = DataFrame(columns=['A','B'])
1604+
df[0] = Series(1,index=range(4))
1605+
df.dtypes
1606+
str(df)
1607+
assert_frame_equal(df,expected)
1608+
1609+
df = DataFrame(columns=['A','B'])
1610+
df.loc[:,0] = Series(1,index=range(4))
1611+
df.dtypes
1612+
str(df)
1613+
assert_frame_equal(df,expected)
1614+
15951615
def test_cache_updating(self):
15961616
# GH 4939, make sure to update the cache on setitem
15971617

0 commit comments

Comments
 (0)