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