4
4
5
5
import pandas as pd
6
6
from pandas .core .accessor import CachedAccessor
7
- from pandas .core .indexes .accessors import (CombinedDatetimelikeProperties ,
8
- DatetimeProperties ,
9
- PeriodProperties )
7
+ from pandas .core .indexes .accessors import (
8
+ CombinedDatetimelikeProperties ,
9
+ DatetimeProperties ,
10
+ PeriodProperties ,
11
+ )
10
12
from pandas .core .strings import StringMethods
11
13
from pandas .util ._decorators import doc
12
14
13
- _str_boolean_methods = set ([
14
- 'contains' ,
15
- 'endswith' ,
16
- 'isalnum' ,
17
- 'isalpha' ,
18
- 'isdecimal' ,
19
- 'isdigit' ,
20
- 'islower' ,
21
- 'isnumeric' ,
22
- 'isspace' ,
23
- 'istitle' ,
24
- 'isupper' ,
25
- 'match' ,
26
- 'startswith' ,
27
- ])
28
-
29
- _date_boolean_methods = set ([
30
- 'is_leap_year' ,
31
- 'is_month_end' ,
32
- 'is_month_start' ,
33
- 'is_quarter_end' ,
34
- 'is_quarter_start' ,
35
- 'is_year_end' ,
36
- 'is_year_start' ,
37
- ])
15
+ _str_boolean_methods = set (
16
+ [
17
+ 'contains' ,
18
+ 'endswith' ,
19
+ 'isalnum' ,
20
+ 'isalpha' ,
21
+ 'isdecimal' ,
22
+ 'isdigit' ,
23
+ 'islower' ,
24
+ 'isnumeric' ,
25
+ 'isspace' ,
26
+ 'istitle' ,
27
+ 'isupper' ,
28
+ 'match' ,
29
+ 'startswith' ,
30
+ ]
31
+ )
32
+
33
+ _date_boolean_methods = set (
34
+ [
35
+ 'is_leap_year' ,
36
+ 'is_month_end' ,
37
+ 'is_month_start' ,
38
+ 'is_quarter_end' ,
39
+ 'is_quarter_start' ,
40
+ 'is_year_end' ,
41
+ 'is_year_start' ,
42
+ ]
43
+ )
38
44
39
45
40
46
class StringSelectMethods (StringMethods ):
@@ -47,11 +53,14 @@ def __init__(self, *args, **kwargs):
47
53
super ().__init__ (self ._series , * args [1 :], ** kwargs )
48
54
49
55
def __getattribute__ (self , attr ):
50
- if (not attr .startswith ("_" ) and # noqa
51
- inspect .isroutine (getattr (StringMethods , attr , None ))
52
- and attr not in _str_boolean_methods ): # noqa
53
- raise NotImplementedError ("Boolean selection with this method "
54
- "does not make sense." )
56
+ if (
57
+ not attr .startswith ("_" )
58
+ and inspect .isroutine (getattr (StringMethods , attr , None )) # noqa
59
+ and attr not in _str_boolean_methods
60
+ ): # noqa
61
+ raise NotImplementedError (
62
+ "Boolean selection with this method " "does not make sense."
63
+ )
55
64
else :
56
65
return super ().__getattribute__ (attr )
57
66
@@ -69,11 +78,16 @@ def __init__(self, parent, *args, **kwargs):
69
78
super ().__init__ (* args , ** kwargs )
70
79
71
80
def __getattribute__ (self , attr ):
72
- if (not attr .startswith ("_" ) and # noqa
73
- inspect .isroutine (getattr (DatetimeProperties , attr , None ))
74
- and attr not in _date_boolean_methods ): # noqa
75
- raise NotImplementedError ("Boolean selection with this method "
76
- "does not make sense." )
81
+ if (
82
+ not attr .startswith ("_" )
83
+ and inspect .isroutine ( # noqa
84
+ getattr (DatetimeProperties , attr , None )
85
+ )
86
+ and attr not in _date_boolean_methods
87
+ ): # noqa
88
+ raise NotImplementedError (
89
+ "Boolean selection with this method " "does not make sense."
90
+ )
77
91
elif attr in _date_boolean_methods :
78
92
idx = super ().__getattribute__ (attr )
79
93
return self ._parent_frame .loc [idx ]
@@ -96,17 +110,16 @@ def __new__(cls, series):
96
110
properties = super ().__new__ (cls , series ._series )
97
111
if isinstance (properties , DatetimeProperties ):
98
112
return SelectDatetimeProperties (
99
- series ._parent ,
100
- properties ._parent ,
101
- properties .orig )
113
+ series ._parent , properties ._parent , properties .orig
114
+ )
102
115
elif isinstance (properties , PeriodProperties ):
103
116
return SelectPeriodProperties (
104
- series ._frame ,
105
- properties ._parent ,
106
- properties .orig
117
+ series ._frame , properties ._parent , properties .orig
107
118
)
108
- raise AttributeError ("Can only use select.dt accessor on"
109
- "datetimelike and periodlike values." )
119
+ raise AttributeError (
120
+ "Can only use select.dt accessor on"
121
+ "datetimelike and periodlike values."
122
+ )
110
123
111
124
112
125
def selector_wrapper (klass , method_name ):
@@ -119,6 +132,7 @@ def selector(self, *args, **kwargs):
119
132
series = self ._series
120
133
idx = getattr (klass , method_name )(series , * args , ** kwargs )
121
134
return self ._parent .loc [idx ]
135
+
122
136
return selector
123
137
124
138
0 commit comments