diff --git a/doc/source/whatsnew/v0.22.0.txt b/doc/source/whatsnew/v0.22.0.txt index 841eec69d41ba..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,6 +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`) +- :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 ^^^ diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 938fd7130faa5..d17767d662bb4 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -983,20 +983,32 @@ 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. """ from pandas import Series - return Series(self._to_embed(), - index=self._shallow_copy(), - name=self.name) + + if index is None: + index = self._shallow_copy() + if name is None: + name = self.name + + 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 290c77dd7f040..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): + 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,15 +949,24 @@ def to_series(self, keep_tz=False): 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 ------- Series """ from pandas import Series - return Series(self._to_embed(keep_tz), - index=self._shallow_copy(), - 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), 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 c1ee18526cc01..a4b72f29aa65f 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -50,6 +50,25 @@ def test_to_series(self): assert s.index is not idx assert s.name == idx.name + def test_to_series_with_arguments(self): + # GH18699 + + # index kwarg + 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 + + # name kwarg + 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()