@@ -169,7 +169,7 @@ class CategoricalDtypeType(type):
169
169
@register_extension_dtype
170
170
class CategoricalDtype (PandasExtensionDtype , ExtensionDtype ):
171
171
"""
172
- Type for categorical data with the categories and orderedness
172
+ Type for categorical data with the categories and orderedness.
173
173
174
174
.. versionchanged:: 0.21.0
175
175
@@ -334,6 +334,9 @@ def _finalize(self, categories, ordered, fastpath=False):
334
334
self ._ordered = ordered
335
335
336
336
def __setstate__ (self , state ):
337
+ # for pickle compat. __get_state__ is defined in the
338
+ # PandasExtensionDtype superclass and uses the public properties to
339
+ # pickle -> need to set the settable private ones here (see GH26067)
337
340
self ._categories = state .pop ('categories' , None )
338
341
self ._ordered = state .pop ('ordered' , False )
339
342
@@ -570,13 +573,40 @@ def _is_boolean(self):
570
573
571
574
@register_extension_dtype
572
575
class DatetimeTZDtype (PandasExtensionDtype , ExtensionDtype ):
573
-
574
576
"""
575
- A np.dtype duck-typed class, suitable for holding a custom datetime with tz
576
- dtype.
577
+ An ExtensionDtype for timezone-aware datetime data.
578
+
579
+ **This is not an actual numpy dtype**, but a duck type.
580
+
581
+ Parameters
582
+ ----------
583
+ unit : str, default "ns"
584
+ The precision of the datetime data. Currently limited
585
+ to ``"ns"``.
586
+ tz : str, int, or datetime.tzinfo
587
+ The timezone.
588
+
589
+ Attributes
590
+ ----------
591
+ unit
592
+ tz
593
+
594
+ Methods
595
+ -------
596
+ None
577
597
578
- THIS IS NOT A REAL NUMPY DTYPE, but essentially a sub-class of
579
- np.datetime64[ns]
598
+ Raises
599
+ ------
600
+ pytz.UnknownTimeZoneError
601
+ When the requested timezone cannot be found.
602
+
603
+ Examples
604
+ --------
605
+ >>> pd.DatetimeTZDtype(tz='UTC')
606
+ datetime64[ns, UTC]
607
+
608
+ >>> pd.DatetimeTZDtype(tz='dateutil/US/Central')
609
+ datetime64[ns, tzfile('/usr/share/zoneinfo/US/Central')]
580
610
"""
581
611
type = Timestamp # type: Type[Timestamp]
582
612
kind = 'M' # type: str_type
@@ -589,30 +619,6 @@ class DatetimeTZDtype(PandasExtensionDtype, ExtensionDtype):
589
619
_cache = {} # type: Dict[str_type, PandasExtensionDtype]
590
620
591
621
def __init__ (self , unit = "ns" , tz = None ):
592
- """
593
- An ExtensionDtype for timezone-aware datetime data.
594
-
595
- Parameters
596
- ----------
597
- unit : str, default "ns"
598
- The precision of the datetime data. Currently limited
599
- to ``"ns"``.
600
- tz : str, int, or datetime.tzinfo
601
- The timezone.
602
-
603
- Raises
604
- ------
605
- pytz.UnknownTimeZoneError
606
- When the requested timezone cannot be found.
607
-
608
- Examples
609
- --------
610
- >>> pd.core.dtypes.dtypes.DatetimeTZDtype(tz='UTC')
611
- datetime64[ns, UTC]
612
-
613
- >>> pd.core.dtypes.dtypes.DatetimeTZDtype(tz='dateutil/US/Central')
614
- datetime64[ns, tzfile('/usr/share/zoneinfo/US/Central')]
615
- """
616
622
if isinstance (unit , DatetimeTZDtype ):
617
623
unit , tz = unit .unit , unit .tz
618
624
@@ -718,17 +724,40 @@ def __eq__(self, other):
718
724
str (self .tz ) == str (other .tz ))
719
725
720
726
def __setstate__ (self , state ):
721
- # for pickle compat.
727
+ # for pickle compat. __get_state__ is defined in the
728
+ # PandasExtensionDtype superclass and uses the public properties to
729
+ # pickle -> need to set the settable private ones here (see GH26067)
722
730
self ._tz = state ['tz' ]
723
731
self ._unit = state ['unit' ]
724
732
725
733
726
734
@register_extension_dtype
727
735
class PeriodDtype (ExtensionDtype , PandasExtensionDtype ):
728
736
"""
729
- A Period duck-typed class, suitable for holding a period with freq dtype.
737
+ An ExtensionDtype for Period data.
738
+
739
+ **This is not an actual numpy dtype**, but a duck type.
740
+
741
+ Parameters
742
+ ----------
743
+ freq : str or DateOffset
744
+ The frequency of this PeriodDtype
745
+
746
+ Attributes
747
+ ----------
748
+ freq
730
749
731
- THIS IS NOT A REAL NUMPY DTYPE, but essentially a sub-class of np.int64.
750
+ Methods
751
+ -------
752
+ None
753
+
754
+ Examples
755
+ --------
756
+ >>> pd.PeriodDtype(freq='D')
757
+ period[D]
758
+
759
+ >>> pd.PeriodDtype(freq=pd.offsets.MonthEnd())
760
+ period[M]
732
761
"""
733
762
type = Period # type: Type[Period]
734
763
kind = 'O' # type: str_type
@@ -751,7 +780,9 @@ def __new__(cls, freq=None):
751
780
752
781
elif freq is None :
753
782
# empty constructor for pickle compat
754
- return object .__new__ (cls )
783
+ u = object .__new__ (cls )
784
+ u ._freq = None
785
+ return u
755
786
756
787
if not isinstance (freq , ABCDateOffset ):
757
788
freq = cls ._parse_dtype_strict (freq )
@@ -760,10 +791,15 @@ def __new__(cls, freq=None):
760
791
return cls ._cache [freq .freqstr ]
761
792
except KeyError :
762
793
u = object .__new__ (cls )
763
- u .freq = freq
794
+ u ._freq = freq
764
795
cls ._cache [freq .freqstr ] = u
765
796
return u
766
797
798
+ @property
799
+ def freq (self ):
800
+ """The frequency object of this PeriodDtype."""
801
+ return self ._freq
802
+
767
803
@classmethod
768
804
def _parse_dtype_strict (cls , freq ):
769
805
if isinstance (freq , str ):
@@ -817,6 +853,12 @@ def __eq__(self, other):
817
853
818
854
return isinstance (other , PeriodDtype ) and self .freq == other .freq
819
855
856
+ def __setstate__ (self , state ):
857
+ # for pickle compat. __get_state__ is defined in the
858
+ # PandasExtensionDtype superclass and uses the public properties to
859
+ # pickle -> need to set the settable private ones here (see GH26067)
860
+ self ._freq = state ['freq' ]
861
+
820
862
@classmethod
821
863
def is_dtype (cls , dtype ):
822
864
"""
@@ -849,9 +891,27 @@ def construct_array_type(cls):
849
891
@register_extension_dtype
850
892
class IntervalDtype (PandasExtensionDtype , ExtensionDtype ):
851
893
"""
852
- A Interval duck-typed class, suitable for holding an interval
894
+ An ExtensionDtype for Interval data.
853
895
854
- THIS IS NOT A REAL NUMPY DTYPE
896
+ **This is not an actual numpy dtype**, but a duck type.
897
+
898
+ Parameters
899
+ ----------
900
+ subtype : str, np.dtype
901
+ The dtype of the Interval bounds.
902
+
903
+ Attributes
904
+ ----------
905
+ subtype
906
+
907
+ Methods
908
+ -------
909
+ None
910
+
911
+ Examples
912
+ --------
913
+ >>> pd.IntervalDtype(subtype='int64')
914
+ interval[int64]
855
915
"""
856
916
name = 'interval'
857
917
kind = None # type: Optional[str_type]
@@ -863,11 +923,6 @@ class IntervalDtype(PandasExtensionDtype, ExtensionDtype):
863
923
_cache = {} # type: Dict[str_type, PandasExtensionDtype]
864
924
865
925
def __new__ (cls , subtype = None ):
866
- """
867
- Parameters
868
- ----------
869
- subtype : the dtype of the Interval
870
- """
871
926
from pandas .core .dtypes .common import (
872
927
is_categorical_dtype , is_string_dtype , pandas_dtype )
873
928
@@ -877,7 +932,7 @@ def __new__(cls, subtype=None):
877
932
# we are called as an empty constructor
878
933
# generally for pickle compat
879
934
u = object .__new__ (cls )
880
- u .subtype = None
935
+ u ._subtype = None
881
936
return u
882
937
elif (isinstance (subtype , str ) and
883
938
subtype .lower () == 'interval' ):
@@ -903,10 +958,15 @@ def __new__(cls, subtype=None):
903
958
return cls ._cache [str (subtype )]
904
959
except KeyError :
905
960
u = object .__new__ (cls )
906
- u .subtype = subtype
961
+ u ._subtype = subtype
907
962
cls ._cache [str (subtype )] = u
908
963
return u
909
964
965
+ @property
966
+ def subtype (self ):
967
+ """The dtype of the Interval bounds."""
968
+ return self ._subtype
969
+
910
970
@classmethod
911
971
def construct_array_type (cls ):
912
972
"""
@@ -963,6 +1023,12 @@ def __eq__(self, other):
963
1023
from pandas .core .dtypes .common import is_dtype_equal
964
1024
return is_dtype_equal (self .subtype , other .subtype )
965
1025
1026
+ def __setstate__ (self , state ):
1027
+ # for pickle compat. __get_state__ is defined in the
1028
+ # PandasExtensionDtype superclass and uses the public properties to
1029
+ # pickle -> need to set the settable private ones here (see GH26067)
1030
+ self ._subtype = state ['subtype' ]
1031
+
966
1032
@classmethod
967
1033
def is_dtype (cls , dtype ):
968
1034
"""
0 commit comments