Skip to content

BUG: Pass kwargs from Index.to_series to pd.Series #18707

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

Merged
merged 4 commits into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
^^^
Expand Down
20 changes: 16 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""

Copy link
Contributor

Choose a reason for hiding this comment

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

instead of this, simply list the kwargs that we accept, name=None, index=None and pass them thru.

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):
"""
Expand Down
17 changes: 13 additions & 4 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Contributor

Choose a reason for hiding this comment

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

you can do this in 1 test

# 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()
Expand Down