@@ -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
0 commit comments