From 88f144b1f7726f07d786462d82e43871a9bad122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Lipt=C3=A1k?= Date: Thu, 19 May 2016 19:19:51 -0400 Subject: [PATCH] Correct ValueError invalid type promotion exception --- doc/source/whatsnew/v0.18.2.txt | 3 +++ pandas/core/indexing.py | 9 ++++++--- pandas/tests/series/test_indexing.py | 10 ++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.18.2.txt b/doc/source/whatsnew/v0.18.2.txt index a77bdcec2ce7a..670c24555213d 100644 --- a/doc/source/whatsnew/v0.18.2.txt +++ b/doc/source/whatsnew/v0.18.2.txt @@ -58,6 +58,9 @@ API changes - Calls to ``.sample()`` will respect the random seed set via ``numpy.random.seed(n)`` (:issue:`13161`) +- Correct ValueError invalid type promotion exception (:issue:`13234`) + + .. _whatsnew_0182.api.tolist: ``Series.tolist()`` will now return Python types diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index acb0675247a78..9485f50ed07f1 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -336,9 +336,12 @@ def _setitem_with_indexer(self, indexer, value): # this preserves dtype of the value new_values = Series([value])._values if len(self.obj._values): - new_values = np.concatenate([self.obj._values, - new_values]) - + try: + new_values = np.concatenate([self.obj._values, + new_values]) + except TypeError: + new_values = np.concatenate([self.obj.asobject, + new_values]) self.obj._data = self.obj._constructor( new_values, index=new_index, name=self.obj.name)._data self.obj._maybe_update_cacher(clear=True) diff --git a/pandas/tests/series/test_indexing.py b/pandas/tests/series/test_indexing.py index 5ed3fda7d0b8f..29cd887c7075f 100644 --- a/pandas/tests/series/test_indexing.py +++ b/pandas/tests/series/test_indexing.py @@ -287,6 +287,16 @@ def test_getitem_generator(self): assert_series_equal(result, expected) assert_series_equal(result2, expected) + def test_type_promotion(self): + # GH12599 + s = pd.Series() + s["a"] = pd.Timestamp("2016-01-01") + s["b"] = 3.0 + s["c"] = "foo" + expected = Series([pd.Timestamp("2016-01-01"), 3.0, "foo"], + index=["a", "b", "c"]) + assert_series_equal(s, expected) + def test_getitem_boolean_object(self): # using column from DataFrame