-
- Notifications
You must be signed in to change notification settings - Fork 19.2k
TST: add tests for keeping dtype in Series.update #23604
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
Changes from 17 commits
925237a 9a74273 8a0a7ed 3718979 31317ea e448675 241de90 528f36f 66b535f c180b48 437a025 be15e28 71655ec b736c4b 8f78023 a5610a2 9168f4e 57466dd 0f50a25 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -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) | ||
| | @@ -116,8 +116,60 @@ 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_values', [ | ||
| [61, 63], # int | ||
| [61., 63.], # float, but can be cast to int | ||
| [61.1, 63.1], # float, cannot be cast to int | ||
| [(61,), (63,)] # object | ||
| ], ids=['int', 'float_castable', 'float', 'object']) | ||
| @pytest.mark.parametrize('caller_dtype', ['int32', 'int64', | ||
| 'float32', 'float64', object]) | ||
| def test_update_dtypes(self, caller_dtype, other_values): | ||
| caller_values = [10, 11, 12] | ||
| s = Series(caller_values, dtype=caller_dtype) | ||
| other = Series(other_values, index=[1, 3]) | ||
| s.update(other) | ||
| | ||
| expected_values = [caller_values[0], other_values[0], caller_values[2]] | ||
| try: | ||
| # we keep original dtype whenever possible | ||
| ||
| expected = Series(expected_values, dtype=caller_dtype) | ||
| except ValueError: | ||
| expected = Series(expected_values) | ||
gfyoung marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| assert_series_equal(s, expected) | ||
| | ||
| @pytest.mark.parametrize('other_values, caller_dtype, expected', [ | ||
| ||
| # other_values is int | ||
| ([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_values is float, but can be cast to int | ||
| ([61., 63.], 'int64', pd.Series([10, 61, 12], dtype='int64')), | ||
| ([61., 63.], float, pd.Series([10., 61., 12.])), | ||
| ([61., 63.], object, pd.Series([10, 61., 12], dtype=object)), | ||
| # other_values is float, cannot be cast to int | ||
| ([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_values is object, cannot be cast | ||
| ([(61,), (63,)], 'int64', pd.Series([10, (61,), 12])), | ||
| ([(61,), (63,)], float, pd.Series([10., (61,), 12.])), | ||
| ([(61,), (63,)], object, pd.Series([10, (61,), 12])) | ||
| ]) | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_no_try_catch(self, other_values, | ||
| caller_dtype, expected): | ||
| caller_values = [10, 11, 12] | ||
| s = Series(caller_values, dtype=caller_dtype) | ||
| other = Series(other_values, index=[1, 3]) | ||
| s.update(other) | ||
| | ||
| assert_series_equal(s, expected) | ||
| | ||
| def test_concat_empty_series_dtypes_roundtrips(self): | ||
| | ||
| | ||
Uh oh!
There was an error while loading. Please reload this page.