From 294dd2e5699073d8b363f58daf71d6c90b670dd3 Mon Sep 17 00:00:00 2001 From: makbigc Date: Tue, 27 Aug 2019 22:34:24 +0800 Subject: [PATCH 01/22] astype keeps nan when converting into string --- doc/source/whatsnew/v1.0.0.rst | 2 +- pandas/core/internals/blocks.py | 5 ++++- pandas/tests/frame/test_dtypes.py | 8 ++++---- pandas/tests/reductions/test_reductions.py | 6 ++++-- pandas/tests/series/test_dtypes.py | 12 +++++++++++- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index a5af4e727391a..d8d6aec62e1f1 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -200,7 +200,7 @@ Indexing Missing ^^^^^^^ -- +- When converting into string, :meth:`Series.astype` will keep ``np.nan`` as missing value (:issue:`25353`) - MultiIndex diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 01399a23e810e..ce82c1496fb06 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -618,10 +618,13 @@ def _astype(self, dtype, copy=False, errors="raise", **kwargs): else: values = self.get_values(dtype=dtype) + skipna = kwargs.pop("skipna", True) # _astype_nansafe works fine with 1-d only vals1d = values.ravel() try: - values = astype_nansafe(vals1d, dtype, copy=True, **kwargs) + values = astype_nansafe( + vals1d, dtype, copy=True, skipna=skipna, **kwargs + ) except (ValueError, TypeError): # e.g. astype_nansafe can fail on object-dtype of strings # trying to convert to float diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index 00be13b1c0e72..58137dacfa2c9 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -627,13 +627,13 @@ def test_astype_str(self): assert_frame_equal(result, expected) def test_astype_str_float(self): - # see gh-11302 + # GH 25353 result = DataFrame([np.NaN]).astype(str) - expected = DataFrame(["nan"]) - + expected = DataFrame([np.nan], dtype=object) assert_frame_equal(result, expected) - result = DataFrame([1.12345678901234567890]).astype(str) + # see gh-11302 + result = DataFrame([1.12345678901234567890]).astype(str) # < 1.14 truncates # >= 1.14 preserves the full repr val = "1.12345678901" if _np_version_under1p14 else "1.1234567890123457" diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 05ebff4387908..ba80928560a6b 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -1105,7 +1105,10 @@ def test_mode_numerical_nan(self, dropna, expected): @pytest.mark.parametrize( "dropna, expected1, expected2, expected3", - [(True, ["b"], ["bar"], ["nan"]), (False, ["b"], [np.nan], ["nan"])], + [ + (True, ["b"], ["bar"], Series(["bar"])), + (False, ["b"], [np.nan], Series([np.nan], dtype=object)), + ], ) def test_mode_str_obj(self, dropna, expected1, expected2, expected3): # Test string and object types. @@ -1127,7 +1130,6 @@ def test_mode_str_obj(self, dropna, expected1, expected2, expected3): s = Series(data, dtype=object).astype(str) result = s.mode(dropna) - expected3 = Series(expected3, dtype=str) tm.assert_series_equal(result, expected3) @pytest.mark.parametrize( diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index 9be79bf93ece7..3c192260b7d98 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -147,7 +147,7 @@ def test_astype_datetime64tz(self): ) def test_astype_str_map(self, dtype, series): # see gh-4405 - result = series.astype(dtype) + result = series.astype(dtype, skipna=False) expected = series.map(str) tm.assert_series_equal(result, expected) @@ -171,6 +171,16 @@ def test_astype_str_cast(self): expected = Series([str("1 days 00:00:00.000000000")]) tm.assert_series_equal(s, expected) + @pytest.mark.parametrize( + "skipna, expected", + [(True, Series(["1", "a", np.nan])), (False, Series(["1", "a", "nan"]))], + ) + def test_astype_str(self, skipna, expected): + # GH 25353 + ser = Series([1, "a", np.nan]) + result = ser.astype(str, skipna=skipna) + tm.assert_series_equal(result, expected) + def test_astype_unicode(self): # see gh-7758: A bit of magic is required to set # default encoding to utf-8 From 39b429425ae8de8290830605ca74a0b21a389171 Mon Sep 17 00:00:00 2001 From: makbigc Date: Tue, 10 Sep 2019 10:33:36 +0800 Subject: [PATCH 02/22] Move the entry to API change section and make it prominent --- doc/source/whatsnew/v1.0.0.rst | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index d8d6aec62e1f1..d13bb8cc69a2b 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -73,6 +73,28 @@ Backwards incompatible API changes pd.arrays.IntervalArray.from_tuples([(0, 1), (2, 3)]) +.. _whatsnew_0250.api_breaking.series_string_conversion: + +String conversion of Series with nan +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + When converting into string, :meth:`Series.astype` will not convert ``np.nan`` into string and keep it as missing value (:issue:`25353`) + + *pandas 0.25.x* + +.. code-block:: ipython + + In [1]: pd.Series(['foo', np.nan]).astype(str) + Out[2]: + 0 foo + 1 nan + dtype: object + +*pandas 1.0.0* + +.. ipython:: python + pd.Series(['foo', np.nan]).astype(str) + .. _whatsnew_1000.api.other: Other API changes @@ -200,7 +222,7 @@ Indexing Missing ^^^^^^^ -- When converting into string, :meth:`Series.astype` will keep ``np.nan`` as missing value (:issue:`25353`) +- - MultiIndex From b82e02f1649e7f8d1e681cb0423e7a60351c3532 Mon Sep 17 00:00:00 2001 From: makbigc Date: Mon, 16 Sep 2019 15:34:49 +0800 Subject: [PATCH 03/22] Fix entry in v1.0 --- doc/source/whatsnew/v1.0.0.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index d13bb8cc69a2b..4b1f027e1e5bb 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -78,9 +78,9 @@ Backwards incompatible API changes String conversion of Series with nan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - When converting into string, :meth:`Series.astype` will not convert ``np.nan`` into string and keep it as missing value (:issue:`25353`) +:meth:Series.astype(str) previously would coerce a np.nan to the string nan. Now pandas will preserve the missing value indicator (:issue:`25353`) - *pandas 0.25.x* +*Previous behavior*: .. code-block:: ipython @@ -90,7 +90,7 @@ String conversion of Series with nan 1 nan dtype: object -*pandas 1.0.0* +*New behavior*: .. ipython:: python pd.Series(['foo', np.nan]).astype(str) From 55d1cf71bebdbe13353ddca0239e9812d20b598f Mon Sep 17 00:00:00 2001 From: makbigc Date: Thu, 19 Sep 2019 22:51:36 +0800 Subject: [PATCH 04/22] Move the whatsnew entry to deprecation section --- doc/source/whatsnew/v1.0.0.rst | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 4b1f027e1e5bb..1abeae2f0acdc 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -73,7 +73,19 @@ Backwards incompatible API changes pd.arrays.IntervalArray.from_tuples([(0, 1), (2, 3)]) -.. _whatsnew_0250.api_breaking.series_string_conversion: +.. _whatsnew_1000.api.other: + +Other API changes +^^^^^^^^^^^^^^^^^ + +- :meth:`pandas.api.types.infer_dtype` will now return "integer-na" for integer and ``np.nan`` mix (:issue:`27283`) +- :meth:`MultiIndex.from_arrays` will no longer infer names from arrays if ``names=None`` is explicitly provided (:issue:`27292`) +- + +.. _whatsnew_1000.deprecations: + +Deprecations +~~~~~~~~~~~~ String conversion of Series with nan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -95,23 +107,6 @@ String conversion of Series with nan .. ipython:: python pd.Series(['foo', np.nan]).astype(str) -.. _whatsnew_1000.api.other: - -Other API changes -^^^^^^^^^^^^^^^^^ - -- :meth:`pandas.api.types.infer_dtype` will now return "integer-na" for integer and ``np.nan`` mix (:issue:`27283`) -- :meth:`MultiIndex.from_arrays` will no longer infer names from arrays if ``names=None`` is explicitly provided (:issue:`27292`) -- - -.. _whatsnew_1000.deprecations: - -Deprecations -~~~~~~~~~~~~ - -- -- - .. _whatsnew_1000.prior_deprecations: From f44afcfa0fc517efe0716ffd32faf5964fc46bf6 Mon Sep 17 00:00:00 2001 From: makbigc Date: Sun, 6 Oct 2019 23:21:15 +0800 Subject: [PATCH 05/22] Add skipna keyword into astype --- pandas/core/generic.py | 18 ++++++++++++++---- pandas/core/internals/blocks.py | 8 ++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index ddbdb48ab0441..9d98a4f53ad48 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5776,7 +5776,7 @@ def _to_dict_of_blocks(self, copy=True): for k, v, in self._data.to_dict(copy=copy).items() } - def astype(self, dtype, copy=True, errors="raise"): + def astype(self, dtype, copy=True, errors="raise", skipna=True): """ Cast a pandas object to a specified dtype ``dtype``. @@ -5798,6 +5798,9 @@ def astype(self, dtype, copy=True, errors="raise"): - ``ignore`` : suppress exceptions. On error return original object. .. versionadded:: 0.20.0 + skipna : bool, default True + Exclude NA/null values in type conversion. + Returns ------- @@ -5884,7 +5887,7 @@ def astype(self, dtype, copy=True, errors="raise"): "the key in Series dtype mappings." ) new_type = dtype[self.name] - return self.astype(new_type, copy, errors) + return self.astype(new_type, copy, errors, skipna) for col_name in dtype.keys(): if col_name not in self: @@ -5896,7 +5899,12 @@ def astype(self, dtype, copy=True, errors="raise"): for col_name, col in self.items(): if col_name in dtype: results.append( - col.astype(dtype=dtype[col_name], copy=copy, errors=errors) + col.astype( + dtype=dtype[col_name], + copy=copy, + errors=errors, + skipna=skipna + ) ) else: results.append(results.append(col.copy() if copy else col)) @@ -5911,7 +5919,9 @@ def astype(self, dtype, copy=True, errors="raise"): else: # else, only a single dtype is given - new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors) + new_data = self._data.astype( + dtype=dtype, copy=copy, errors=errors, skipna=skipna + ) return self._constructor(new_data).__finalize__(self) # GH 19920: retain column metadata after concat diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index b7d167beeb0b4..3187389bd7157 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -536,10 +536,10 @@ def f(mask, val, idx): return self.split_and_operate(None, f, False) - def astype(self, dtype, copy=False, errors="raise", **kwargs): - return self._astype(dtype, copy=copy, errors=errors, **kwargs) + def astype(self, dtype, copy=False, errors="raise", skipna=True, **kwargs): + return self._astype(dtype, copy=copy, errors=errors, skipna=skipna, **kwargs) - def _astype(self, dtype, copy=False, errors="raise", **kwargs): + def _astype(self, dtype, copy=False, errors="raise", skipna=True, **kwargs): """Coerce to the new type Parameters @@ -606,7 +606,7 @@ def _astype(self, dtype, copy=False, errors="raise", **kwargs): else: values = self.get_values(dtype=dtype) - skipna = kwargs.pop("skipna", True) + #skipna = kwargs.pop("skipna", True) # _astype_nansafe works fine with 1-d only vals1d = values.ravel() try: From b5428c3aa8fef1f07d43357b662b7f0f3c2956b0 Mon Sep 17 00:00:00 2001 From: makbigc Date: Mon, 7 Oct 2019 19:19:56 +0800 Subject: [PATCH 06/22] Fix linting adn docstring format --- pandas/core/generic.py | 1 - pandas/core/internals/blocks.py | 1 - 2 files changed, 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 9d98a4f53ad48..267cded04c089 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5801,7 +5801,6 @@ def astype(self, dtype, copy=True, errors="raise", skipna=True): skipna : bool, default True Exclude NA/null values in type conversion. - Returns ------- casted : same type as caller diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 3187389bd7157..58b3b2e9f40bf 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -606,7 +606,6 @@ def _astype(self, dtype, copy=False, errors="raise", skipna=True, **kwargs): else: values = self.get_values(dtype=dtype) - #skipna = kwargs.pop("skipna", True) # _astype_nansafe works fine with 1-d only vals1d = values.ravel() try: From d48dc29cc60006032b54ab5b9b3dec1c332ebaa0 Mon Sep 17 00:00:00 2001 From: makbigc Date: Thu, 5 Dec 2019 13:49:12 +0800 Subject: [PATCH 07/22] fix black format --- pandas/core/internals/blocks.py | 8 +++++++- pandas/tests/frame/test_dtypes.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index f77930e9446cb..efe32930e5abb 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -523,7 +523,13 @@ def f(mask, val, idx): return self.split_and_operate(None, f, False) - def astype(self, dtype, copy: bool = False, errors: str = "raise", skipna=True, **kwargs): + def astype( + self, + dtype, copy: bool = False, + errors: str = "raise", + skipna=True, + **kwargs + ): return self._astype(dtype, copy=copy, errors=errors, skipna=skipna, **kwargs) def _astype(self, dtype, copy=False, errors="raise", skipna=True, **kwargs): diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index 4198d0d32f2f8..b801c6afd2c26 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -583,7 +583,7 @@ def test_astype_str_float(self): # GH 25353 result = DataFrame([np.NaN]).astype(str) expected = DataFrame([np.nan], dtype=object) - assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) # see gh-11302 result = DataFrame([1.12345678901234567890]).astype(str) From b8724e5e600970352e10633402f1b95e4cd75c67 Mon Sep 17 00:00:00 2001 From: makbigc Date: Sat, 7 Dec 2019 14:59:46 +0800 Subject: [PATCH 08/22] Add skipna parameter into DatetimeBlock.astype and Block.astype --- pandas/core/internals/blocks.py | 9 +++++++-- pandas/core/internals/managers.py | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index efe32930e5abb..58527aafbcfda 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -2150,7 +2150,12 @@ def _maybe_coerce_values(self, values): assert isinstance(values, np.ndarray), type(values) return values - def astype(self, dtype, copy: bool = False, errors: str = "raise"): + def astype( + self, + dtype, copy: bool = False, + errors: str = "raise", + skipna: bool = True + ): """ these automatically copy, so copy=True has no effect raise on an except if raise == True @@ -2166,7 +2171,7 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"): return self.make_block(values) # delegate - return super().astype(dtype=dtype, copy=copy, errors=errors) + return super().astype(dtype=dtype, copy=copy, errors=errors, skipna=skipna) def _can_hold_element(self, element: Any) -> bool: tipo = maybe_infer_dtype_type(element) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index f312b88d9a0bc..e7d31d80ea0be 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -550,8 +550,20 @@ def fillna(self, **kwargs): def downcast(self, **kwargs): return self.apply("downcast", **kwargs) - def astype(self, dtype, copy: bool = False, errors: str = "raise"): - return self.apply("astype", dtype=dtype, copy=copy, errors=errors) + def astype( + self, + dtype, + copy: bool = False, + errors: str = "raise", + skipna: bool = True + ): + return self.apply( + "astype", + dtype=dtype, + copy=copy, + errors=errors, + skipna=skipna + ) def convert(self, **kwargs): return self.apply("convert", **kwargs) From 7f48697944821bbe33f5fe7ab83531f9ecb4696a Mon Sep 17 00:00:00 2001 From: makbigc Date: Sat, 7 Dec 2019 15:00:35 +0800 Subject: [PATCH 09/22] Fix black format --- pandas/core/generic.py | 2 +- pandas/core/internals/blocks.py | 15 +++------------ pandas/core/internals/managers.py | 12 ++---------- 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index d8c7ef62bf2a3..80c00e35cde5c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5696,7 +5696,7 @@ def astype(self, dtype, copy: bool_t = True, errors: str = "raise", skipna=True) dtype=dtype[col_name], copy=copy, errors=errors, - skipna=skipna + skipna=skipna, ) ) else: diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 58527aafbcfda..a18e7977151fa 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -524,11 +524,7 @@ def f(mask, val, idx): return self.split_and_operate(None, f, False) def astype( - self, - dtype, copy: bool = False, - errors: str = "raise", - skipna=True, - **kwargs + self, dtype, copy: bool = False, errors: str = "raise", skipna=True, **kwargs ): return self._astype(dtype, copy=copy, errors=errors, skipna=skipna, **kwargs) @@ -602,9 +598,7 @@ def _astype(self, dtype, copy=False, errors="raise", skipna=True, **kwargs): # _astype_nansafe works fine with 1-d only vals1d = values.ravel() try: - values = astype_nansafe( - vals1d, dtype, copy=True, skipna=skipna - ) + values = astype_nansafe(vals1d, dtype, copy=True, skipna=skipna) except (ValueError, TypeError): # e.g. astype_nansafe can fail on object-dtype of strings # trying to convert to float @@ -2151,10 +2145,7 @@ def _maybe_coerce_values(self, values): return values def astype( - self, - dtype, copy: bool = False, - errors: str = "raise", - skipna: bool = True + self, dtype, copy: bool = False, errors: str = "raise", skipna: bool = True ): """ these automatically copy, so copy=True has no effect diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index e7d31d80ea0be..10cfb45d92141 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -551,18 +551,10 @@ def downcast(self, **kwargs): return self.apply("downcast", **kwargs) def astype( - self, - dtype, - copy: bool = False, - errors: str = "raise", - skipna: bool = True + self, dtype, copy: bool = False, errors: str = "raise", skipna: bool = True ): return self.apply( - "astype", - dtype=dtype, - copy=copy, - errors=errors, - skipna=skipna + "astype", dtype=dtype, copy=copy, errors=errors, skipna=skipna ) def convert(self, **kwargs): From 9c624c57aad38687d5f0e0490ab9989bdef72c5a Mon Sep 17 00:00:00 2001 From: makbigc Date: Sat, 7 Dec 2019 16:31:29 +0800 Subject: [PATCH 10/22] resolve mypy issue --- pandas/core/internals/blocks.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index a18e7977151fa..5cb5226523b8b 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -524,7 +524,12 @@ def f(mask, val, idx): return self.split_and_operate(None, f, False) def astype( - self, dtype, copy: bool = False, errors: str = "raise", skipna=True, **kwargs + self, + dtype, + copy: bool = False, + errors: str = "raise", + skipna: bool = True, + **kwargs, ): return self._astype(dtype, copy=copy, errors=errors, skipna=skipna, **kwargs) @@ -2145,7 +2150,7 @@ def _maybe_coerce_values(self, values): return values def astype( - self, dtype, copy: bool = False, errors: str = "raise", skipna: bool = True + self, dtype, copy: bool = False, errors: str = "raise", skipna=True, **kwargs ): """ these automatically copy, so copy=True has no effect @@ -2162,7 +2167,9 @@ def astype( return self.make_block(values) # delegate - return super().astype(dtype=dtype, copy=copy, errors=errors, skipna=skipna) + return super().astype( + dtype=dtype, copy=copy, errors=errors, skipna=skipna, **kwargs + ) def _can_hold_element(self, element: Any) -> bool: tipo = maybe_infer_dtype_type(element) From 369c64153b71364da7cb7ab2047d7d3d8f7c6c27 Mon Sep 17 00:00:00 2001 From: makbigc Date: Mon, 9 Dec 2019 12:13:35 +0800 Subject: [PATCH 11/22] Remove kwarg parameter in astype function --- pandas/core/internals/blocks.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 5cb5226523b8b..a5ce0762f8ff6 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -529,11 +529,10 @@ def astype( copy: bool = False, errors: str = "raise", skipna: bool = True, - **kwargs, ): - return self._astype(dtype, copy=copy, errors=errors, skipna=skipna, **kwargs) + return self._astype(dtype, copy=copy, errors=errors, skipna=skipna) - def _astype(self, dtype, copy=False, errors="raise", skipna=True, **kwargs): + def _astype(self, dtype, copy=False, errors="raise", skipna=True): """Coerce to the new type Parameters @@ -544,6 +543,8 @@ def _astype(self, dtype, copy=False, errors="raise", skipna=True, **kwargs): errors : str, {'raise', 'ignore'}, default 'ignore' - ``raise`` : allow exceptions to be raised - ``ignore`` : suppress exceptions. On error return original object + skipna : bool, default True + Exclude NA/null values in type conversion. Returns ------- @@ -2150,7 +2151,7 @@ def _maybe_coerce_values(self, values): return values def astype( - self, dtype, copy: bool = False, errors: str = "raise", skipna=True, **kwargs + self, dtype, copy: bool = False, errors: str = "raise", skipna=True ): """ these automatically copy, so copy=True has no effect @@ -2168,7 +2169,7 @@ def astype( # delegate return super().astype( - dtype=dtype, copy=copy, errors=errors, skipna=skipna, **kwargs + dtype=dtype, copy=copy, errors=errors, skipna=skipna ) def _can_hold_element(self, element: Any) -> bool: From 2754677f0ce249bf4804c68e4792f9033cf7fe2b Mon Sep 17 00:00:00 2001 From: makbigc Date: Mon, 9 Dec 2019 13:38:36 +0800 Subject: [PATCH 12/22] Add FutureWarning for string-type conversion --- pandas/core/generic.py | 9 +++++++++ pandas/tests/series/test_dtypes.py | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 80c00e35cde5c..25643a838794d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5672,6 +5672,15 @@ def astype(self, dtype, copy: bool_t = True, errors: str = "raise", skipna=True) 1 2 dtype: int64 """ + if isna(self.values).any(): + msg = ( + "The 'skipna' parameter is added and set True by default " + "which preserves the missing value indicator in type conversion. " + "The type conversion in which the missing value indicator loses " + "its meaning will be deprecated in the future version." + ) + warnings.warn(msg, FutureWarning, stacklevel=2) + if is_dict_like(dtype): if self.ndim == 1: # i.e. Series if len(dtype) > 1 or self.name not in dtype: diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index 8f1477d454d1c..02a46f31364c3 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -162,6 +162,12 @@ def test_astype_str(self, skipna, expected): result = ser.astype(str, skipna=skipna) tm.assert_series_equal(result, expected) + def test_deprecate_astype_str(self): + # GH 25353 + ser = Series([1, "a", np.nan]) + with tm.assert_produces_warning(expected_warning=FutureWarning): + ser.astype(str) + def test_astype_unicode(self): # see gh-7758: A bit of magic is required to set # default encoding to utf-8 From ce39f6aaf354016fad033c004753a88601d3841a Mon Sep 17 00:00:00 2001 From: makbigc Date: Mon, 9 Dec 2019 14:10:45 +0800 Subject: [PATCH 13/22] Fix black format --- pandas/core/internals/blocks.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index a5ce0762f8ff6..f532792744a19 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -524,11 +524,7 @@ def f(mask, val, idx): return self.split_and_operate(None, f, False) def astype( - self, - dtype, - copy: bool = False, - errors: str = "raise", - skipna: bool = True, + self, dtype, copy: bool = False, errors: str = "raise", skipna: bool = True, ): return self._astype(dtype, copy=copy, errors=errors, skipna=skipna) @@ -2150,9 +2146,7 @@ def _maybe_coerce_values(self, values): assert isinstance(values, np.ndarray), type(values) return values - def astype( - self, dtype, copy: bool = False, errors: str = "raise", skipna=True - ): + def astype(self, dtype, copy: bool = False, errors: str = "raise", skipna=True): """ these automatically copy, so copy=True has no effect raise on an except if raise == True @@ -2168,9 +2162,7 @@ def astype( return self.make_block(values) # delegate - return super().astype( - dtype=dtype, copy=copy, errors=errors, skipna=skipna - ) + return super().astype(dtype=dtype, copy=copy, errors=errors, skipna=skipna) def _can_hold_element(self, element: Any) -> bool: tipo = maybe_infer_dtype_type(element) From 35fd58fb2dfe5f7a63b14a286a54ed7ab1713df7 Mon Sep 17 00:00:00 2001 From: makbigc Date: Mon, 9 Dec 2019 18:42:01 +0800 Subject: [PATCH 14/22] Add okwarning to suppress FutureWarning --- doc/source/getting_started/basics.rst | 2 ++ doc/source/user_guide/io.rst | 1 + doc/source/user_guide/sparse.rst | 2 ++ doc/source/user_guide/text.rst | 1 + doc/source/user_guide/timedeltas.rst | 1 + 5 files changed, 7 insertions(+) diff --git a/doc/source/getting_started/basics.rst b/doc/source/getting_started/basics.rst index d489d35dc1226..906e5eb6e0cec 100644 --- a/doc/source/getting_started/basics.rst +++ b/doc/source/getting_started/basics.rst @@ -980,6 +980,7 @@ Passing a ``lambda`` function will yield a ```` named row: Passing a named function will yield that name for the row: .. ipython:: python + :okwarning: def mymean(x): return x.mean() @@ -1111,6 +1112,7 @@ Transforming with a dict Passing a dict of functions will allow selective transforming per column. .. ipython:: python + :okwarning: tsdf.transform({'A': np.abs, 'B': lambda x: x + 1}) diff --git a/doc/source/user_guide/io.rst b/doc/source/user_guide/io.rst index fa47a5944f7bf..eff6bea96e392 100644 --- a/doc/source/user_guide/io.rst +++ b/doc/source/user_guide/io.rst @@ -391,6 +391,7 @@ Or you can use the :func:`~pandas.to_numeric` function to coerce the dtypes after reading in the data, .. ipython:: python + :okwarning: df2 = pd.read_csv(StringIO(data)) df2['col_1'] = pd.to_numeric(df2['col_1'], errors='coerce') diff --git a/doc/source/user_guide/sparse.rst b/doc/source/user_guide/sparse.rst index c258a8840b714..adcaff988887a 100644 --- a/doc/source/user_guide/sparse.rst +++ b/doc/source/user_guide/sparse.rst @@ -26,6 +26,7 @@ The sparse objects exist for memory efficiency reasons. Suppose you had a large, mostly NA ``DataFrame``: .. ipython:: python + :okwarning: df = pd.DataFrame(np.random.randn(10000, 4)) df.iloc[:9998] = np.nan @@ -300,6 +301,7 @@ meth:`Series.sparse.to_coo` is implemented for transforming a ``Series`` with sp The method requires a ``MultiIndex`` with two or more levels. .. ipython:: python + :okwarning: s = pd.Series([3.0, np.nan, 1.0, 3.0, np.nan, np.nan]) s.index = pd.MultiIndex.from_tuples([(1, 2, 'a', 0), diff --git a/doc/source/user_guide/text.rst b/doc/source/user_guide/text.rst index 072871f89bdae..3fca9ceecc851 100644 --- a/doc/source/user_guide/text.rst +++ b/doc/source/user_guide/text.rst @@ -85,6 +85,7 @@ l. For ``StringDtype``, :ref:`string accessor methods` Both outputs are ``Int64`` dtype. Compare that with object-dtype .. ipython:: python + :okwarning: s.astype(object).str.count("a") s.astype(object).dropna().str.count("a") diff --git a/doc/source/user_guide/timedeltas.rst b/doc/source/user_guide/timedeltas.rst index 3439a0a4c13c7..2c60134aba1c3 100644 --- a/doc/source/user_guide/timedeltas.rst +++ b/doc/source/user_guide/timedeltas.rst @@ -237,6 +237,7 @@ or by astyping to a specific timedelta type. These operations yield Series and p Note that division by the NumPy scalar is true division, while astyping is equivalent of floor division. .. ipython:: python + :okwarning: december = pd.Series(pd.date_range('20121201', periods=4)) january = pd.Series(pd.date_range('20130101', periods=4)) From 1d29cd07545336b9cc7b79e1090705e374996613 Mon Sep 17 00:00:00 2001 From: makbigc Date: Mon, 9 Dec 2019 18:46:06 +0800 Subject: [PATCH 15/22] Add :okwarning: in whatsnew to suppress FutureWarning --- doc/source/whatsnew/v0.11.0.rst | 1 + doc/source/whatsnew/v0.13.0.rst | 1 + doc/source/whatsnew/v0.24.0.rst | 2 ++ 3 files changed, 4 insertions(+) diff --git a/doc/source/whatsnew/v0.11.0.rst b/doc/source/whatsnew/v0.11.0.rst index 148ee349b049c..ad3aa735f2b22 100644 --- a/doc/source/whatsnew/v0.11.0.rst +++ b/doc/source/whatsnew/v0.11.0.rst @@ -305,6 +305,7 @@ Furthermore ``datetime64[ns]`` columns are created by default, when passed datet Astype conversion on ``datetime64[ns]`` to ``object``, implicitly converts ``NaT`` to ``np.nan`` .. ipython:: python + :okwarning: s = pd.Series([datetime.datetime(2001, 1, 2, 0, 0) for i in range(3)]) s.dtype diff --git a/doc/source/whatsnew/v0.13.0.rst b/doc/source/whatsnew/v0.13.0.rst index ab48594ddadab..777f8ff7e2879 100644 --- a/doc/source/whatsnew/v0.13.0.rst +++ b/doc/source/whatsnew/v0.13.0.rst @@ -532,6 +532,7 @@ Enhancements is frequency conversion. See :ref:`the docs` for the docs. .. ipython:: python + :okwarning: import datetime td = pd.Series(pd.date_range('20130101', periods=4)) - pd.Series( diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 85de0150a5a28..c37bbf81254a4 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -57,6 +57,7 @@ marker of ``np.nan`` will infer to integer dtype. The display of the ``Series`` Operations on these dtypes will propagate ``NaN`` as other pandas operations. .. ipython:: python + :okwarning: # arithmetic s + 1 @@ -85,6 +86,7 @@ These dtypes can operate as part of a ``DataFrame``. These dtypes can be merged, reshaped, and casted. .. ipython:: python + :okwarning: pd.concat([df[['A']], df[['B', 'C']]], axis=1).dtypes df['A'].astype(float) From 34c51e05a5de508c3287a4840b4bcff4068e5ffc Mon Sep 17 00:00:00 2001 From: makbigc Date: Mon, 9 Dec 2019 18:59:15 +0800 Subject: [PATCH 16/22] Add :okwarning: to suppress FutureWarning --- doc/source/getting_started/basics.rst | 5 ++++- doc/source/user_guide/integer_na.rst | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/source/getting_started/basics.rst b/doc/source/getting_started/basics.rst index 906e5eb6e0cec..09feb73f1ceeb 100644 --- a/doc/source/getting_started/basics.rst +++ b/doc/source/getting_started/basics.rst @@ -974,6 +974,7 @@ On a ``Series``, multiple functions return a ``Series``, indexed by the function Passing a ``lambda`` function will yield a ```` named row: .. ipython:: python + :okwarning: tsdf['A'].agg(['sum', lambda x: x.mean()]) @@ -1035,6 +1036,7 @@ With ``.agg()`` is it possible to easily create a custom describe function, simi to the built in :ref:`describe function `. .. ipython:: python + :okwarning: from functools import partial @@ -1067,7 +1069,6 @@ Transform the entire frame. ``.transform()`` allows input functions as: a NumPy function name or a user defined function. .. ipython:: python - :okwarning: tsdf.transform(np.abs) tsdf.transform('abs') @@ -1094,6 +1095,7 @@ The first level will be the original frame column names; the second level will be the names of the transforming functions. .. ipython:: python + :okwarning: tsdf.transform([np.abs, lambda x: x + 1]) @@ -1101,6 +1103,7 @@ Passing multiple functions to a Series will yield a DataFrame. The resulting column names will be the transforming functions. .. ipython:: python + :okwarning: tsdf['A'].transform([np.abs, lambda x: x + 1]) diff --git a/doc/source/user_guide/integer_na.rst b/doc/source/user_guide/integer_na.rst index 77568f3bcb244..00ed6bbc2ad82 100644 --- a/doc/source/user_guide/integer_na.rst +++ b/doc/source/user_guide/integer_na.rst @@ -113,6 +113,7 @@ These dtypes can operate as part of of ``DataFrame``. These dtypes can be merged & reshaped & casted. .. ipython:: python + :okwarning: pd.concat([df[['A']], df[['B', 'C']]], axis=1).dtypes df['A'].astype(float) From d879778165b1537aaf1d4180fbbad4ef41b3b6a6 Mon Sep 17 00:00:00 2001 From: makbigc Date: Mon, 9 Dec 2019 19:38:57 +0800 Subject: [PATCH 17/22] Add :okwarning: into getting_started/basic.rst --- doc/source/getting_started/basics.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/getting_started/basics.rst b/doc/source/getting_started/basics.rst index 09feb73f1ceeb..4aa306779486e 100644 --- a/doc/source/getting_started/basics.rst +++ b/doc/source/getting_started/basics.rst @@ -1143,6 +1143,7 @@ a single value and returning a single value. For example: df4 = df_orig.copy() .. ipython:: python + :okwarning: df4 From 9765497d823136ef8a71a2a1e83773f601ff45d0 Mon Sep 17 00:00:00 2001 From: makbigc Date: Mon, 9 Dec 2019 21:26:55 +0800 Subject: [PATCH 18/22] Add :okwarning: into integer_na.rst --- doc/source/user_guide/integer_na.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/user_guide/integer_na.rst b/doc/source/user_guide/integer_na.rst index 00ed6bbc2ad82..f3c6fdee55e5a 100644 --- a/doc/source/user_guide/integer_na.rst +++ b/doc/source/user_guide/integer_na.rst @@ -83,6 +83,7 @@ Missing values will be propagated, and the data will be coerced to another dtype if needed. .. ipython:: python + :okwarning: s = pd.Series([1, 2, None], dtype="Int64") From 3057073841f510614bccbeaacb509c6630d40032 Mon Sep 17 00:00:00 2001 From: makbigc Date: Fri, 3 Jan 2020 22:08:32 +0800 Subject: [PATCH 19/22] Remove skipna parameter and set skipna=True in astype_nansafe --- pandas/core/dtypes/cast.py | 2 +- pandas/core/generic.py | 15 +++++---------- pandas/core/internals/blocks.py | 14 ++++++-------- pandas/core/internals/managers.py | 8 ++------ 4 files changed, 14 insertions(+), 25 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 0579c97747bae..88ae337ef784e 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -789,7 +789,7 @@ def conv(r, dtype): return [conv(r, dtype) for r, dtype in zip(result, dtypes)] -def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False): +def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = True): """ Cast the elements of an array to a given dtype a nan-safe manner. diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b75e9e63ea992..32aa83f9ee464 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5499,7 +5499,7 @@ def _to_dict_of_blocks(self, copy: bool_t = True): } def astype( - self: FrameOrSeries, dtype, copy: bool_t = True, errors: str = "raise", skipna=True + self: FrameOrSeries, dtype, copy: bool_t = True, errors: str = "raise" ) -> FrameOrSeries: """ Cast a pandas object to a specified dtype ``dtype``. @@ -5522,8 +5522,6 @@ def astype( - ``ignore`` : suppress exceptions. On error return original object. .. versionadded:: 0.20.0 - skipna : bool, default True - Exclude NA/null values in type conversion. Returns ------- @@ -5604,10 +5602,8 @@ def astype( """ if isna(self.values).any(): msg = ( - "The 'skipna' parameter is added and set True by default " - "which preserves the missing value indicator in type conversion. " - "The type conversion in which the missing value indicator loses " - "its meaning will be deprecated in the future version." + "The meaning of the missing value indicator is preserved " + "by default in the future version." ) warnings.warn(msg, FutureWarning, stacklevel=2) @@ -5619,7 +5615,7 @@ def astype( "the key in Series dtype mappings." ) new_type = dtype[self.name] - return self.astype(new_type, copy, errors, skipna) + return self.astype(new_type, copy, errors) for col_name in dtype.keys(): if col_name not in self: @@ -5635,7 +5631,6 @@ def astype( dtype=dtype[col_name], copy=copy, errors=errors, - skipna=skipna, ) ) else: @@ -5652,7 +5647,7 @@ def astype( else: # else, only a single dtype is given new_data = self._data.astype( - dtype=dtype, copy=copy, errors=errors, skipna=skipna + dtype=dtype, copy=copy, errors=errors ) return self._constructor(new_data).__finalize__(self) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 8076855ab625a..da2529bb1063b 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -532,11 +532,11 @@ def f(mask, val, idx): return self.split_and_operate(None, f, False) def astype( - self, dtype, copy: bool = False, errors: str = "raise", skipna: bool = True, + self, dtype, copy: bool = False, errors: str = "raise" ): - return self._astype(dtype, copy=copy, errors=errors, skipna=skipna) + return self._astype(dtype, copy=copy, errors=errors) - def _astype(self, dtype, copy=False, errors="raise", skipna=True): + def _astype(self, dtype, copy=False, errors="raise"): """Coerce to the new type Parameters @@ -547,8 +547,6 @@ def _astype(self, dtype, copy=False, errors="raise", skipna=True): errors : str, {'raise', 'ignore'}, default 'ignore' - ``raise`` : allow exceptions to be raised - ``ignore`` : suppress exceptions. On error return original object - skipna : bool, default True - Exclude NA/null values in type conversion. Returns ------- @@ -608,7 +606,7 @@ def _astype(self, dtype, copy=False, errors="raise", skipna=True): # _astype_nansafe works fine with 1-d only vals1d = values.ravel() try: - values = astype_nansafe(vals1d, dtype, copy=True, skipna=skipna) + values = astype_nansafe(vals1d, dtype, copy=True) except (ValueError, TypeError): # e.g. astype_nansafe can fail on object-dtype of strings # trying to convert to float @@ -2150,7 +2148,7 @@ def _maybe_coerce_values(self, values): assert isinstance(values, np.ndarray), type(values) return values - def astype(self, dtype, copy: bool = False, errors: str = "raise", skipna=True): + def astype(self, dtype, copy: bool = False, errors: str = "raise"): """ these automatically copy, so copy=True has no effect raise on an except if raise == True @@ -2166,7 +2164,7 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise", skipna=True): return self.make_block(values) # delegate - return super().astype(dtype=dtype, copy=copy, errors=errors, skipna=skipna) + return super().astype(dtype=dtype, copy=copy, errors=errors) def _can_hold_element(self, element: Any) -> bool: tipo = maybe_infer_dtype_type(element) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index c97d429881e1c..0d2e2fbfd8ddd 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -579,12 +579,8 @@ def fillna(self, **kwargs): def downcast(self, **kwargs): return self.apply("downcast", **kwargs) - def astype( - self, dtype, copy: bool = False, errors: str = "raise", skipna: bool = True - ): - return self.apply( - "astype", dtype=dtype, copy=copy, errors=errors, skipna=skipna - ) + def astype(self, dtype, copy: bool = False, errors: str = "raise"): + return self.apply("astype", dtype=dtype, copy=copy, errors=errors) def convert(self, **kwargs): return self.apply("convert", **kwargs) From 59030b123bf7c4164441f99594bdd9a720450146 Mon Sep 17 00:00:00 2001 From: makbigc Date: Fri, 3 Jan 2020 22:09:11 +0800 Subject: [PATCH 20/22] Change test_astype_str_map --- pandas/tests/series/test_dtypes.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index e7b57616dd9ce..2d3218361c2ca 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -119,16 +119,11 @@ def test_astype_datetime64tz(self): tm.assert_series_equal(result, expected) @pytest.mark.parametrize("dtype", [str, np.str_]) - @pytest.mark.parametrize( - "series", - [ - Series([string.digits * 10, tm.rands(63), tm.rands(64), tm.rands(1000)]), - Series([string.digits * 10, tm.rands(63), tm.rands(64), np.nan, 1.0]), - ], - ) def test_astype_str_map(self, dtype, series): # see gh-4405 - result = series.astype(dtype, skipna=False) + series = Series([string.digits * 10, tm.rands(63), + tm.rands(64), tm.rands(1000)]) + result = series.astype(dtype) expected = series.map(str) tm.assert_series_equal(result, expected) @@ -152,14 +147,12 @@ def test_astype_str_cast(self): expected = Series([str("1 days 00:00:00.000000000")]) tm.assert_series_equal(s, expected) - @pytest.mark.parametrize( - "skipna, expected", - [(True, Series(["1", "a", np.nan])), (False, Series(["1", "a", "nan"]))], - ) def test_astype_str(self, skipna, expected): # GH 25353 + pytest.set_trace() ser = Series([1, "a", np.nan]) result = ser.astype(str, skipna=skipna) + expected = Series(["1", "a", np.nan]) tm.assert_series_equal(result, expected) def test_deprecate_astype_str(self): From 11c2015c9f8d8c71af5a2b43da26d7460241a07b Mon Sep 17 00:00:00 2001 From: makbigc Date: Sat, 4 Jan 2020 14:04:43 +0800 Subject: [PATCH 21/22] fix test_astype_str --- pandas/tests/series/test_dtypes.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index 2d3218361c2ca..e2e2bf0bea4e0 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -119,7 +119,7 @@ def test_astype_datetime64tz(self): tm.assert_series_equal(result, expected) @pytest.mark.parametrize("dtype", [str, np.str_]) - def test_astype_str_map(self, dtype, series): + def test_astype_str_map(self, dtype): # see gh-4405 series = Series([string.digits * 10, tm.rands(63), tm.rands(64), tm.rands(1000)]) @@ -147,11 +147,10 @@ def test_astype_str_cast(self): expected = Series([str("1 days 00:00:00.000000000")]) tm.assert_series_equal(s, expected) - def test_astype_str(self, skipna, expected): + def test_astype_str(self): # GH 25353 - pytest.set_trace() ser = Series([1, "a", np.nan]) - result = ser.astype(str, skipna=skipna) + result = ser.astype(str) expected = Series(["1", "a", np.nan]) tm.assert_series_equal(result, expected) From 68c8e85b2ae16356e0d3d49ff271abdc89fa6b1c Mon Sep 17 00:00:00 2001 From: makbigc Date: Sat, 4 Jan 2020 14:05:36 +0800 Subject: [PATCH 22/22] Fix black format --- pandas/core/generic.py | 12 +++--------- pandas/core/internals/blocks.py | 4 +--- pandas/tests/series/test_dtypes.py | 5 +++-- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 32aa83f9ee464..62539d8a66148 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5499,7 +5499,7 @@ def _to_dict_of_blocks(self, copy: bool_t = True): } def astype( - self: FrameOrSeries, dtype, copy: bool_t = True, errors: str = "raise" + self: FrameOrSeries, dtype, copy: bool_t = True, errors: str = "raise" ) -> FrameOrSeries: """ Cast a pandas object to a specified dtype ``dtype``. @@ -5627,11 +5627,7 @@ def astype( for col_name, col in self.items(): if col_name in dtype: results.append( - col.astype( - dtype=dtype[col_name], - copy=copy, - errors=errors, - ) + col.astype(dtype=dtype[col_name], copy=copy, errors=errors,) ) else: results.append(col.copy() if copy else col) @@ -5646,9 +5642,7 @@ def astype( else: # else, only a single dtype is given - new_data = self._data.astype( - dtype=dtype, copy=copy, errors=errors - ) + new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors) return self._constructor(new_data).__finalize__(self) # GH 19920: retain column metadata after concat diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index da2529bb1063b..74aa7d4b850d7 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -531,9 +531,7 @@ def f(mask, val, idx): return self.split_and_operate(None, f, False) - def astype( - self, dtype, copy: bool = False, errors: str = "raise" - ): + def astype(self, dtype, copy: bool = False, errors: str = "raise"): return self._astype(dtype, copy=copy, errors=errors) def _astype(self, dtype, copy=False, errors="raise"): diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index e2e2bf0bea4e0..016793d8823c8 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -121,8 +121,9 @@ def test_astype_datetime64tz(self): @pytest.mark.parametrize("dtype", [str, np.str_]) def test_astype_str_map(self, dtype): # see gh-4405 - series = Series([string.digits * 10, tm.rands(63), - tm.rands(64), tm.rands(1000)]) + series = Series( + [string.digits * 10, tm.rands(63), tm.rands(64), tm.rands(1000)] + ) result = series.astype(dtype) expected = series.map(str) tm.assert_series_equal(result, expected)