Skip to content

Commit 295c278

Browse files
h-vetinarijreback
authored andcommitted
TST: add tests for keeping dtype in Series.update (pandas-dev#23604)
1 parent 34e8d52 commit 295c278

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

pandas/tests/series/test_combine_concat.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import pandas as pd
1111
from pandas import DataFrame, DatetimeIndex, Series, compat, date_range
1212
import pandas.util.testing as tm
13-
from pandas.util.testing import assert_series_equal
13+
from pandas.util.testing import assert_frame_equal, assert_series_equal
1414

1515

16-
class TestSeriesCombine():
16+
class TestSeriesCombine(object):
1717

1818
def test_append(self, datetime_series, string_series, object_series):
1919
appendedSeries = string_series.append(object_series)
@@ -116,8 +116,40 @@ def test_update(self):
116116
df = DataFrame([{"a": 1}, {"a": 3, "b": 2}])
117117
df['c'] = np.nan
118118

119-
# this will fail as long as series is a sub-class of ndarray
120-
# df['c'].update(Series(['foo'],index=[0])) #####
119+
df['c'].update(Series(['foo'], index=[0]))
120+
expected = DataFrame([[1, np.nan, 'foo'], [3, 2., np.nan]],
121+
columns=['a', 'b', 'c'])
122+
assert_frame_equal(df, expected)
123+
124+
@pytest.mark.parametrize('other, dtype, expected', [
125+
# other is int
126+
([61, 63], 'int32', pd.Series([10, 61, 12], dtype='int32')),
127+
([61, 63], 'int64', pd.Series([10, 61, 12])),
128+
([61, 63], float, pd.Series([10., 61., 12.])),
129+
([61, 63], object, pd.Series([10, 61, 12], dtype=object)),
130+
# other is float, but can be cast to int
131+
([61., 63.], 'int32', pd.Series([10, 61, 12], dtype='int32')),
132+
([61., 63.], 'int64', pd.Series([10, 61, 12])),
133+
([61., 63.], float, pd.Series([10., 61., 12.])),
134+
([61., 63.], object, pd.Series([10, 61., 12], dtype=object)),
135+
# others is float, cannot be cast to int
136+
([61.1, 63.1], 'int32', pd.Series([10., 61.1, 12.])),
137+
([61.1, 63.1], 'int64', pd.Series([10., 61.1, 12.])),
138+
([61.1, 63.1], float, pd.Series([10., 61.1, 12.])),
139+
([61.1, 63.1], object, pd.Series([10, 61.1, 12], dtype=object)),
140+
# other is object, cannot be cast
141+
([(61,), (63,)], 'int32', pd.Series([10, (61,), 12])),
142+
([(61,), (63,)], 'int64', pd.Series([10, (61,), 12])),
143+
([(61,), (63,)], float, pd.Series([10., (61,), 12.])),
144+
([(61,), (63,)], object, pd.Series([10, (61,), 12]))
145+
])
146+
def test_update_dtypes(self, other, dtype, expected):
147+
148+
s = Series([10, 11, 12], dtype=dtype)
149+
other = Series(other, index=[1, 3])
150+
s.update(other)
151+
152+
assert_series_equal(s, expected)
121153

122154
def test_concat_empty_series_dtypes_roundtrips(self):
123155

0 commit comments

Comments
 (0)