Skip to content

Commit f8a11dd

Browse files
gliptakjreback
authored andcommitted
ERR: Correct ValueError invalid type promotion exception
closes #12599 Author: Gábor Lipták <[email protected]> Closes #13234 from gliptak/invalidtypepromotion and squashes the following commits: 88f144b [Gábor Lipták] Correct ValueError invalid type promotion exception
1 parent 19ebee5 commit f8a11dd

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

doc/source/whatsnew/v0.18.2.txt

+30-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ API changes
5959
- An ``UnsupportedFunctionCall`` error is now raised if numpy ufuncs like ``np.mean`` are called on groupby or resample objects (:issue:`12811`)
6060
- Calls to ``.sample()`` will respect the random seed set via ``numpy.random.seed(n)`` (:issue:`13161`)
6161

62-
6362
.. _whatsnew_0182.api.tolist:
6463

6564
``Series.tolist()`` will now return Python types
@@ -88,6 +87,36 @@ New Behavior:
8887
type(s.tolist()[0])
8988

9089

90+
.. _whatsnew_0182.api.promote:
91+
92+
``Series`` type promotoion on assignment
93+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
94+
95+
A ``Series`` will now correctly promote its dtype with assignment with incompat values to the current dtype (:issue:`13234`)
96+
97+
98+
.. ipython:: python
99+
100+
s = pd.Series()
101+
102+
Previous Behavior:
103+
104+
.. code-block:: ipython
105+
106+
In [2]: s["a"] = pd.Timestamp("2016-01-01")
107+
108+
In [3]: s["b"] = 3.0
109+
TypeError: invalid type promotion
110+
111+
New Behavior:
112+
113+
.. ipython:: python
114+
115+
s["a"] = pd.Timestamp("2016-01-01")
116+
s["b"] = 3.0
117+
s
118+
s.dtype
119+
91120
.. _whatsnew_0182.api.to_datetime_coerce:
92121

93122
``.to_datetime()`` when coercing

pandas/core/indexing.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,12 @@ def _setitem_with_indexer(self, indexer, value):
336336
# this preserves dtype of the value
337337
new_values = Series([value])._values
338338
if len(self.obj._values):
339-
new_values = np.concatenate([self.obj._values,
340-
new_values])
341-
339+
try:
340+
new_values = np.concatenate([self.obj._values,
341+
new_values])
342+
except TypeError:
343+
new_values = np.concatenate([self.obj.asobject,
344+
new_values])
342345
self.obj._data = self.obj._constructor(
343346
new_values, index=new_index, name=self.obj.name)._data
344347
self.obj._maybe_update_cacher(clear=True)

pandas/tests/series/test_indexing.py

+10
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@ def test_getitem_generator(self):
287287
assert_series_equal(result, expected)
288288
assert_series_equal(result2, expected)
289289

290+
def test_type_promotion(self):
291+
# GH12599
292+
s = pd.Series()
293+
s["a"] = pd.Timestamp("2016-01-01")
294+
s["b"] = 3.0
295+
s["c"] = "foo"
296+
expected = Series([pd.Timestamp("2016-01-01"), 3.0, "foo"],
297+
index=["a", "b", "c"])
298+
assert_series_equal(s, expected)
299+
290300
def test_getitem_boolean_object(self):
291301
# using column from DataFrame
292302

0 commit comments

Comments
 (0)