Skip to content

Commit 3204652

Browse files
jrebackjtratner
authored andcommitted
COMPAT: allow string concats to datetimeindex only (int/float are invalid)
1 parent 2173680 commit 3204652

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Bug Fixes
7373
~~~~~~~~~
7474
- Bug in Series replace with timestamp dict (:issue:`5797`)
7575
- read_csv/read_table now respects the `prefix` kwarg (:issue:`5732`).
76+
- Bug with insert of strings into DatetimeIndex (:issue:`5818`, :issue:`5819`)
7677

7778
pandas 0.13.0
7879
-------------

pandas/core/indexing.py

-1
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,6 @@ def _safe_append_to_index(index, key):
14551455

14561456
# raise here as this is basically an unsafe operation and we want
14571457
# it to be obvious that you are doing something wrong
1458-
14591458
raise ValueError("unsafe appending to index of type {0} with a key "
14601459
"{1}".format(index.__class__.__name__, key))
14611460

pandas/sparse/tests/test_sparse.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,10 @@ def test_icol(self):
10841084

10851085
def test_set_value(self):
10861086

1087-
# this is invalid because it is not a valid type for this index
1088-
self.assertRaises(ValueError, self.frame.set_value, 'foobar', 'B', 1.5)
1087+
# ok as the index gets conver to object
1088+
frame = self.frame.copy()
1089+
res = frame.set_value('foobar', 'B', 1.5)
1090+
self.assert_(res.index.dtype == 'object')
10891091

10901092
res = self.frame
10911093
res.index = res.index.astype(object)

pandas/tests/test_indexing.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1665,15 +1665,13 @@ def test_partial_set_invalid(self):
16651665

16661666
df = tm.makeTimeDataFrame()
16671667

1668+
# don't allow not string inserts
16681669
def f():
16691670
df.loc[100.0, :] = df.ix[0]
16701671
self.assertRaises(ValueError, f)
16711672
def f():
16721673
df.loc[100,:] = df.ix[0]
16731674
self.assertRaises(ValueError, f)
1674-
def f():
1675-
df.loc['a',:] = df.ix[0]
1676-
self.assertRaises(ValueError, f)
16771675

16781676
def f():
16791677
df.ix[100.0, :] = df.ix[0]
@@ -1682,6 +1680,9 @@ def f():
16821680
df.ix[100,:] = df.ix[0]
16831681
self.assertRaises(ValueError, f)
16841682

1683+
# allow object conversion here
1684+
df.loc['a',:] = df.ix[0]
1685+
16851686
def test_partial_set_empty(self):
16861687

16871688
# GH5226

pandas/tseries/index.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1548,9 +1548,11 @@ def insert(self, loc, item):
15481548
self[loc:].asi8))
15491549
return DatetimeIndex(new_index, freq='infer')
15501550
except (AttributeError, TypeError):
1551-
# fall back to object index
1552-
return self.asobject.insert(loc, item)
15531551

1552+
# fall back to object index
1553+
if isinstance(item,compat.string_types):
1554+
return self.asobject.insert(loc, item)
1555+
raise TypeError("cannot insert DatetimeIndex with incompatible label")
15541556

15551557
def delete(self, loc):
15561558
"""
@@ -1591,7 +1593,7 @@ def tz_convert(self, tz):
15911593
def tz_localize(self, tz, infer_dst=False):
15921594
"""
15931595
Localize tz-naive DatetimeIndex to given time zone (using pytz)
1594-
1596+
15951597
Parameters
15961598
----------
15971599
tz : string or pytz.timezone

0 commit comments

Comments
 (0)