From 05dfe3e228c246db4902ff339e5a106e2486f70e Mon Sep 17 00:00:00 2001 From: Aly Sivji Date: Sat, 9 Dec 2017 16:10:57 -0600 Subject: [PATCH 1/4] BUG: Pass kwargs in Index.to_series() to pd.Series() --- pandas/core/indexes/base.py | 10 +++++++--- pandas/core/indexes/datetimes.py | 12 ++++++++---- pandas/tests/indexes/common.py | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 938fd7130faa5..64c50b910f183 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -994,9 +994,13 @@ def to_series(self, **kwargs): """ from pandas import Series - return Series(self._to_embed(), - index=self._shallow_copy(), - name=self.name) + + if 'index' not in kwargs: + kwargs['index'] = self._shallow_copy() + if 'name' not in kwargs: + kwargs['name'] = self.name + + return Series(self._to_embed(), **kwargs) def to_frame(self, index=True): """ diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 290c77dd7f040..c239e7a213a82 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -927,7 +927,7 @@ def _get_time_micros(self): values = self._local_timestamps() return fields.get_time_micros(values) - def to_series(self, keep_tz=False): + def to_series(self, keep_tz=False, **kwargs): """ Create a Series with both index and values equal to the index keys useful with map for returning an indexer based on an index @@ -955,9 +955,13 @@ def to_series(self, keep_tz=False): Series """ from pandas import Series - return Series(self._to_embed(keep_tz), - index=self._shallow_copy(), - name=self.name) + + if 'index' not in kwargs: + kwargs['index'] = self._shallow_copy() + if 'name' not in kwargs: + kwargs['name'] = self.name + + return Series(self._to_embed(keep_tz), **kwargs) def _to_embed(self, keep_tz=False, dtype=None): """ diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index c1ee18526cc01..09e6b71a04d90 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -50,6 +50,24 @@ def test_to_series(self): assert s.index is not idx assert s.name == idx.name + def test_to_series_with_index(self): + # GH18699 + idx = self.create_index() + s = idx.to_series(index=idx) + + assert s.values is not idx.values + assert s.index is idx + assert s.name == idx.name + + def test_to_series_with_name(self): + # GH18699 + idx = self.create_index() + s = idx.to_series(name='__test') + + assert s.values is not idx.values + assert s.index is not idx + assert s.name != idx.name + def test_to_frame(self): # see gh-15230 idx = self.create_index() From 55cd605d06008ec0e6fed94a0359ae3802ff3270 Mon Sep 17 00:00:00 2001 From: Aly Sivji Date: Sat, 9 Dec 2017 18:20:17 -0600 Subject: [PATCH 2/4] Add whatsnew entry --- doc/source/whatsnew/v0.22.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.22.0.txt b/doc/source/whatsnew/v0.22.0.txt index 841eec69d41ba..65599f2f1b917 100644 --- a/doc/source/whatsnew/v0.22.0.txt +++ b/doc/source/whatsnew/v0.22.0.txt @@ -272,6 +272,7 @@ Indexing - Bug in :func:`IntervalIndex.symmetric_difference` where the symmetric difference with a non-``IntervalIndex`` did not raise (:issue:`18475`) - Bug in indexing a datetimelike ``Index`` that raised ``ValueError`` instead of ``IndexError`` (:issue:`18386`). - Bug in tz-aware :class:`DatetimeIndex` where addition/subtraction with a :class:`TimedeltaIndex` or array with ``dtype='timedelta64[ns]'`` was incorrect (:issue:`17558`) +- Bug in :func:`Index.to_series` where kwargs were not passed to :class:`Series` constructor (:issue:`18699`) I/O ^^^ From 81cb61aa83513269d80b72292ea5c58a78199134 Mon Sep 17 00:00:00 2001 From: Aly Sivji Date: Sun, 10 Dec 2017 11:36:53 -0600 Subject: [PATCH 3/4] Change to_series() to only allow index and name params --- pandas/core/indexes/base.py | 20 ++++++++++++++------ pandas/core/indexes/datetimes.py | 17 +++++++++++------ pandas/tests/indexes/common.py | 7 ++++--- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 64c50b910f183..d17767d662bb4 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -983,11 +983,19 @@ def _format_attrs(self): attrs.append(('length', len(self))) return attrs - def to_series(self, **kwargs): + def to_series(self, index=None, name=None): """ Create a Series with both index and values equal to the index keys useful with map for returning an indexer based on an index + Parameters + ---------- + index : Index, optional + index of resulting Series. If None, defaults to original index + name : string, optional + name of resulting Series. If None, defaults to name of original + index + Returns ------- Series : dtype will be based on the type of the Index values. @@ -995,12 +1003,12 @@ def to_series(self, **kwargs): from pandas import Series - if 'index' not in kwargs: - kwargs['index'] = self._shallow_copy() - if 'name' not in kwargs: - kwargs['name'] = self.name + if index is None: + index = self._shallow_copy() + if name is None: + name = self.name - return Series(self._to_embed(), **kwargs) + return Series(self._to_embed(), index=index, name=name) def to_frame(self, index=True): """ diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index c239e7a213a82..b7c1b60f77650 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -927,7 +927,7 @@ def _get_time_micros(self): values = self._local_timestamps() return fields.get_time_micros(values) - def to_series(self, keep_tz=False, **kwargs): + def to_series(self, keep_tz=False, index=None, name=None): """ Create a Series with both index and values equal to the index keys useful with map for returning an indexer based on an index @@ -949,6 +949,11 @@ def to_series(self, keep_tz=False, **kwargs): Series will have a datetime64[ns] dtype. TZ aware objects will have the tz removed. + index : Index, optional + index of resulting Series. If None, defaults to original index + name : string, optional + name of resulting Series. If None, defaults to name of original + index Returns ------- @@ -956,12 +961,12 @@ def to_series(self, keep_tz=False, **kwargs): """ from pandas import Series - if 'index' not in kwargs: - kwargs['index'] = self._shallow_copy() - if 'name' not in kwargs: - kwargs['name'] = self.name + if index is None: + index = self._shallow_copy() + if name is None: + name = self.name - return Series(self._to_embed(keep_tz), **kwargs) + return Series(self._to_embed(keep_tz), index=index, name=name) def _to_embed(self, keep_tz=False, dtype=None): """ diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 09e6b71a04d90..a4b72f29aa65f 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -50,8 +50,10 @@ def test_to_series(self): assert s.index is not idx assert s.name == idx.name - def test_to_series_with_index(self): + def test_to_series_with_arguments(self): # GH18699 + + # index kwarg idx = self.create_index() s = idx.to_series(index=idx) @@ -59,8 +61,7 @@ def test_to_series_with_index(self): assert s.index is idx assert s.name == idx.name - def test_to_series_with_name(self): - # GH18699 + # name kwarg idx = self.create_index() s = idx.to_series(name='__test') From ec05208ba2f24cb5c9da5134bbc1e1081f5fb5a3 Mon Sep 17 00:00:00 2001 From: Aly Sivji Date: Sun, 10 Dec 2017 11:46:25 -0600 Subject: [PATCH 4/4] Update whatsnew --- doc/source/whatsnew/v0.22.0.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.22.0.txt b/doc/source/whatsnew/v0.22.0.txt index 65599f2f1b917..220e55a1f8ee9 100644 --- a/doc/source/whatsnew/v0.22.0.txt +++ b/doc/source/whatsnew/v0.22.0.txt @@ -189,7 +189,7 @@ Other API Changes - :func:`pandas.DataFrame.merge` no longer casts a ``float`` column to ``object`` when merging on ``int`` and ``float`` columns (:issue:`16572`) - The default NA value for :class:`UInt64Index` has changed from 0 to ``NaN``, which impacts methods that mask with NA, such as ``UInt64Index.where()`` (:issue:`18398`) - Refactored ``setup.py`` to use ``find_packages`` instead of explicitly listing out all subpackages (:issue:`18535`) -- Rearranged the order of keyword arguments in :func:`read_excel()` to align with :func:`read_csv()` (:pr:`16672`) +- Rearranged the order of keyword arguments in :func:`read_excel()` to align with :func:`read_csv()` (:issue:`16672`) - :func:`pandas.merge` now raises a ``ValueError`` when trying to merge on incompatible data types (:issue:`9780`) .. _whatsnew_0220.deprecations: @@ -272,7 +272,8 @@ Indexing - Bug in :func:`IntervalIndex.symmetric_difference` where the symmetric difference with a non-``IntervalIndex`` did not raise (:issue:`18475`) - Bug in indexing a datetimelike ``Index`` that raised ``ValueError`` instead of ``IndexError`` (:issue:`18386`). - Bug in tz-aware :class:`DatetimeIndex` where addition/subtraction with a :class:`TimedeltaIndex` or array with ``dtype='timedelta64[ns]'`` was incorrect (:issue:`17558`) -- Bug in :func:`Index.to_series` where kwargs were not passed to :class:`Series` constructor (:issue:`18699`) +- :func:`Index.to_series` now accepts ``index`` and ``name`` kwargs (:issue:`18699`) +- :func:`DatetimeIndex.to_series` now accepts ``index`` and ``name`` kwargs (:issue:`18699`) I/O ^^^