Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: add tests for keeping dtype in Series.update #23604

Merged
merged 19 commits into from
Nov 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions pandas/tests/series/test_combine_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import pandas as pd
from pandas import DataFrame, DatetimeIndex, Series, compat, date_range
import pandas.util.testing as tm
from pandas.util.testing import assert_series_equal
from pandas.util.testing import assert_frame_equal, assert_series_equal


class TestSeriesCombine():
class TestSeriesCombine(object):

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

# this will fail as long as series is a sub-class of ndarray
# df['c'].update(Series(['foo'],index=[0])) #####
df['c'].update(Series(['foo'], index=[0]))
expected = DataFrame([[1, np.nan, 'foo'], [3, 2., np.nan]],
columns=['a', 'b', 'c'])
assert_frame_equal(df, expected)

@pytest.mark.parametrize('other, dtype, expected', [
# other is int
([61, 63], 'int32', pd.Series([10, 61, 12], dtype='int32')),
([61, 63], 'int64', pd.Series([10, 61, 12])),
([61, 63], float, pd.Series([10., 61., 12.])),
([61, 63], object, pd.Series([10, 61, 12], dtype=object)),
# other is float, but can be cast to int
([61., 63.], 'int32', pd.Series([10, 61, 12], dtype='int32')),
([61., 63.], 'int64', pd.Series([10, 61, 12])),
([61., 63.], float, pd.Series([10., 61., 12.])),
([61., 63.], object, pd.Series([10, 61., 12], dtype=object)),
# others is float, cannot be cast to int
([61.1, 63.1], 'int32', pd.Series([10., 61.1, 12.])),
([61.1, 63.1], 'int64', pd.Series([10., 61.1, 12.])),
([61.1, 63.1], float, pd.Series([10., 61.1, 12.])),
([61.1, 63.1], object, pd.Series([10, 61.1, 12], dtype=object)),
# other is object, cannot be cast
([(61,), (63,)], 'int32', pd.Series([10, (61,), 12])),
([(61,), (63,)], 'int64', pd.Series([10, (61,), 12])),
([(61,), (63,)], float, pd.Series([10., (61,), 12.])),
([(61,), (63,)], object, pd.Series([10, (61,), 12]))
])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback, this PR is not meant to add the same test twice in different ways, I'm just showing what your review requirement (to avoid try-except) would mean.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a MUCH better test. It is very explicit. remove the other, rename and ping.

def test_update_dtypes(self, other, dtype, expected):

s = Series([10, 11, 12], dtype=dtype)
other = Series(other, index=[1, 3])
s.update(other)

assert_series_equal(s, expected)

def test_concat_empty_series_dtypes_roundtrips(self):

Expand Down