Skip to content

Commit 88d3eed

Browse files
committed
ENH: Add Index.to_frame method
Closes gh-15230.
1 parent f9ba6fe commit 88d3eed

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed

doc/source/api.rst

+3
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,7 @@ Conversion
13761376
Index.tolist
13771377
Index.to_datetime
13781378
Index.to_series
1379+
Index.to_frame
13791380

13801381
Sorting
13811382
~~~~~~~
@@ -1591,6 +1592,7 @@ Conversion
15911592
DatetimeIndex.to_perioddelta
15921593
DatetimeIndex.to_pydatetime
15931594
DatetimeIndex.to_series
1595+
DatetimeIndex.to_frame
15941596

15951597
TimedeltaIndex
15961598
--------------
@@ -1623,6 +1625,7 @@ Conversion
16231625
TimedeltaIndex.round
16241626
TimedeltaIndex.floor
16251627
TimedeltaIndex.ceil
1628+
TimedeltaIndex.to_frame
16261629

16271630
.. currentmodule:: pandas
16281631

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ New features
3131
- Added ``skipna`` parameter to :func:`~pandas.api.types.infer_dtype` to
3232
support type inference in the presence of missing values (:issue:`17059`).
3333
- :class:`~pandas.Resampler.nearest` is added to support nearest-neighbor upsampling (:issue:`17496`).
34+
- :class:`~pandas.Index~` has added support for a ``to_frame`` method (:issue:`15230`)
3435

3536
.. _whatsnew_0210.enhancements.infer_objects:
3637

pandas/core/indexes/base.py

+23
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,29 @@ def to_series(self, **kwargs):
10051005
index=self._shallow_copy(),
10061006
name=self.name)
10071007

1008+
def to_frame(self, index=True):
1009+
"""
1010+
Create a DataFrame with a column containing the Index.
1011+
1012+
.. versionadded:: 0.21.0
1013+
1014+
Parameters
1015+
----------
1016+
index : boolean, default True
1017+
Set the index of the returned DataFrame as self.
1018+
1019+
Returns
1020+
-------
1021+
DataFrame : a DataFrame containing the original Index data.
1022+
"""
1023+
1024+
from pandas import DataFrame
1025+
result = DataFrame(self._shallow_copy(), columns=[self.name or 0])
1026+
1027+
if index:
1028+
result.index = self
1029+
return result
1030+
10081031
def _to_embed(self, keep_tz=False):
10091032
"""
10101033
*this is an internal non-public method*

pandas/core/indexes/datetimes.py

+39
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,45 @@ def to_series(self, keep_tz=False):
915915
index=self._shallow_copy(),
916916
name=self.name)
917917

918+
def to_frame(self, index=True, keep_tz=False):
919+
"""
920+
Create a DataFrame with a column containing the DatetimeIndex.
921+
922+
.. versionadded:: 0.21.0
923+
924+
Parameters
925+
----------
926+
index : boolean, default True
927+
Set the index of the returned DataFrame as self.
928+
929+
keep_tz : optional, defaults False.
930+
return the data keeping the timezone.
931+
932+
If keep_tz is True:
933+
934+
If the timezone is not set, the resulting
935+
Series will have a datetime64[ns] dtype.
936+
937+
Otherwise the DataFrame will have an datetime64[ns, tz] dtype;
938+
the tz will be preserved.
939+
940+
If keep_tz is False:
941+
942+
DataFrame will have a datetime64[ns] dtype. TZ aware
943+
objects will have the tz removed.
944+
945+
Returns
946+
-------
947+
DataFrame : a DataFrame containing the original DatetimeIndex data.
948+
"""
949+
950+
from pandas import DataFrame
951+
result = DataFrame(self._to_embed(keep_tz), columns=[self.name or 0])
952+
953+
if index:
954+
result.index = self
955+
return result
956+
918957
def _to_embed(self, keep_tz=False):
919958
"""
920959
return an array repr of this object, potentially casting to object

pandas/core/indexes/multi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1010,14 +1010,14 @@ def _to_safe_for_reshape(self):
10101010

10111011
def to_frame(self, index=True):
10121012
"""
1013-
Create a DataFrame with the columns the levels of the MultiIndex
1013+
Create a DataFrame with the columns and levels of the MultiIndex.
10141014
10151015
.. versionadded:: 0.20.0
10161016
10171017
Parameters
10181018
----------
10191019
index : boolean, default True
1020-
return this MultiIndex as the index
1020+
Set the index of the returned DataFrame as self.
10211021
10221022
Returns
10231023
-------

pandas/tests/indexes/common.py

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

54+
def test_to_frame(self):
55+
# see gh-15230
56+
idx = self.create_index()
57+
name = idx.name or 0
58+
59+
df = idx.to_frame()
60+
61+
assert df.index is idx
62+
assert len(df.columns) == 1
63+
assert df.columns[0] == name
64+
assert df[name].values is not idx.values
65+
66+
df = idx.to_frame(index=False)
67+
assert df.index is not idx
68+
5469
def test_shift(self):
5570

5671
# GH8083 test the base class for shift

0 commit comments

Comments
 (0)