Skip to content

Commit 54eaca8

Browse files
alysivjijreback
authored andcommitted
BUG: Pass kwargs from Index.to_series to pd.Series (#18707)
1 parent 73e73db commit 54eaca8

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

doc/source/whatsnew/v0.22.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ Indexing
279279
- Bug in :func:`IntervalIndex.symmetric_difference` where the symmetric difference with a non-``IntervalIndex`` did not raise (:issue:`18475`)
280280
- Bug in indexing a datetimelike ``Index`` that raised ``ValueError`` instead of ``IndexError`` (:issue:`18386`).
281281
- Bug in tz-aware :class:`DatetimeIndex` where addition/subtraction with a :class:`TimedeltaIndex` or array with ``dtype='timedelta64[ns]'`` was incorrect (:issue:`17558`)
282+
- :func:`Index.to_series` now accepts ``index`` and ``name`` kwargs (:issue:`18699`)
283+
- :func:`DatetimeIndex.to_series` now accepts ``index`` and ``name`` kwargs (:issue:`18699`)
282284

283285
I/O
284286
^^^

pandas/core/indexes/base.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -983,20 +983,32 @@ def _format_attrs(self):
983983
attrs.append(('length', len(self)))
984984
return attrs
985985

986-
def to_series(self, **kwargs):
986+
def to_series(self, index=None, name=None):
987987
"""
988988
Create a Series with both index and values equal to the index keys
989989
useful with map for returning an indexer based on an index
990990
991+
Parameters
992+
----------
993+
index : Index, optional
994+
index of resulting Series. If None, defaults to original index
995+
name : string, optional
996+
name of resulting Series. If None, defaults to name of original
997+
index
998+
991999
Returns
9921000
-------
9931001
Series : dtype will be based on the type of the Index values.
9941002
"""
9951003

9961004
from pandas import Series
997-
return Series(self._to_embed(),
998-
index=self._shallow_copy(),
999-
name=self.name)
1005+
1006+
if index is None:
1007+
index = self._shallow_copy()
1008+
if name is None:
1009+
name = self.name
1010+
1011+
return Series(self._to_embed(), index=index, name=name)
10001012

10011013
def to_frame(self, index=True):
10021014
"""

pandas/core/indexes/datetimes.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ def _get_time_micros(self):
932932
values = self._local_timestamps()
933933
return fields.get_time_micros(values)
934934

935-
def to_series(self, keep_tz=False):
935+
def to_series(self, keep_tz=False, index=None, name=None):
936936
"""
937937
Create a Series with both index and values equal to the index keys
938938
useful with map for returning an indexer based on an index
@@ -954,15 +954,24 @@ def to_series(self, keep_tz=False):
954954
955955
Series will have a datetime64[ns] dtype. TZ aware
956956
objects will have the tz removed.
957+
index : Index, optional
958+
index of resulting Series. If None, defaults to original index
959+
name : string, optional
960+
name of resulting Series. If None, defaults to name of original
961+
index
957962
958963
Returns
959964
-------
960965
Series
961966
"""
962967
from pandas import Series
963-
return Series(self._to_embed(keep_tz),
964-
index=self._shallow_copy(),
965-
name=self.name)
968+
969+
if index is None:
970+
index = self._shallow_copy()
971+
if name is None:
972+
name = self.name
973+
974+
return Series(self._to_embed(keep_tz), index=index, name=name)
966975

967976
def _to_embed(self, keep_tz=False, dtype=None):
968977
"""

pandas/tests/indexes/common.py

+19
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ def test_to_series(self):
5151
assert s.index is not idx
5252
assert s.name == idx.name
5353

54+
def test_to_series_with_arguments(self):
55+
# GH18699
56+
57+
# index kwarg
58+
idx = self.create_index()
59+
s = idx.to_series(index=idx)
60+
61+
assert s.values is not idx.values
62+
assert s.index is idx
63+
assert s.name == idx.name
64+
65+
# name kwarg
66+
idx = self.create_index()
67+
s = idx.to_series(name='__test')
68+
69+
assert s.values is not idx.values
70+
assert s.index is not idx
71+
assert s.name != idx.name
72+
5473
def test_to_frame(self):
5574
# see gh-15230
5675
idx = self.create_index()

0 commit comments

Comments
 (0)