1
- ## datetimelike delegation ##
1
+ """
2
+ datetimelike delegation
3
+ """
2
4
3
5
import numpy as np
4
6
from pandas .core .base import PandasDelegate , NoNewAttributesMixin
8
10
from pandas .tseries .tdi import TimedeltaIndex
9
11
from pandas import tslib
10
12
from pandas .core .common import (_NS_DTYPE , _TD_DTYPE , is_period_arraylike ,
11
- is_datetime_arraylike , is_integer_dtype , is_list_like ,
13
+ is_datetime_arraylike , is_integer_dtype ,
14
+ is_list_like ,
12
15
is_datetime64_dtype , is_datetime64tz_dtype ,
13
16
is_timedelta64_dtype , is_categorical_dtype ,
14
17
get_dtype_kinds , take_1d )
15
18
19
+
16
20
def is_datetimelike (data ):
17
- """ return a boolean if we can be successfully converted to a datetimelike """
21
+ """
22
+ return a boolean if we can be successfully converted to a datetimelike
23
+ """
18
24
try :
19
25
maybe_to_datetimelike (data )
20
26
return True
21
27
except (Exception ):
22
28
pass
23
29
return False
24
30
31
+
25
32
def maybe_to_datetimelike (data , copy = False ):
26
33
"""
27
34
return a DelegatedClass of a Series that is datetimelike
@@ -42,7 +49,8 @@ def maybe_to_datetimelike(data, copy=False):
42
49
from pandas import Series
43
50
44
51
if not isinstance (data , Series ):
45
- raise TypeError ("cannot convert an object of type {0} to a datetimelike index" .format (type (data )))
52
+ raise TypeError ("cannot convert an object of type {0} to a "
53
+ "datetimelike index" .format (type (data )))
46
54
47
55
index = data .index
48
56
name = data .name
@@ -51,22 +59,28 @@ def maybe_to_datetimelike(data, copy=False):
51
59
data = orig .values .categories
52
60
53
61
if is_datetime64_dtype (data .dtype ):
54
- return DatetimeProperties (DatetimeIndex (data , copy = copy , freq = 'infer' ), index , name = name ,
55
- orig = orig )
62
+ return DatetimeProperties (DatetimeIndex (data , copy = copy , freq = 'infer' ),
63
+ index , name = name , orig = orig )
56
64
elif is_datetime64tz_dtype (data .dtype ):
57
- return DatetimeProperties (DatetimeIndex (data , copy = copy , freq = 'infer' , ambiguous = 'infer' ),
65
+ return DatetimeProperties (DatetimeIndex (data , copy = copy , freq = 'infer' ,
66
+ ambiguous = 'infer' ),
58
67
index , data .name , orig = orig )
59
68
elif is_timedelta64_dtype (data .dtype ):
60
- return TimedeltaProperties (TimedeltaIndex (data , copy = copy , freq = 'infer' ), index ,
69
+ return TimedeltaProperties (TimedeltaIndex (data , copy = copy ,
70
+ freq = 'infer' ), index ,
61
71
name = name , orig = orig )
62
72
else :
63
73
if is_period_arraylike (data ):
64
- return PeriodProperties (PeriodIndex (data , copy = copy ), index , name = name , orig = orig )
74
+ return PeriodProperties (PeriodIndex (data , copy = copy ), index ,
75
+ name = name , orig = orig )
65
76
if is_datetime_arraylike (data ):
66
- return DatetimeProperties (DatetimeIndex (data , copy = copy , freq = 'infer' ), index ,
77
+ return DatetimeProperties (DatetimeIndex (data , copy = copy ,
78
+ freq = 'infer' ), index ,
67
79
name = name , orig = orig )
68
80
69
- raise TypeError ("cannot convert an object of type {0} to a datetimelike index" .format (type (data )))
81
+ raise TypeError ("cannot convert an object of type {0} to a "
82
+ "datetimelike index" .format (type (data )))
83
+
70
84
71
85
class Properties (PandasDelegate , NoNewAttributesMixin ):
72
86
@@ -80,7 +94,7 @@ def __init__(self, values, index, name, orig=None):
80
94
def _delegate_property_get (self , name ):
81
95
from pandas import Series
82
96
83
- result = getattr (self .values ,name )
97
+ result = getattr (self .values , name )
84
98
85
99
# maybe need to upcast (ints)
86
100
if isinstance (result , np .ndarray ):
@@ -97,14 +111,16 @@ def _delegate_property_get(self, name):
97
111
result = Series (result , index = self .index , name = self .name )
98
112
99
113
# setting this object will show a SettingWithCopyWarning/Error
100
- result .is_copy = ("modifications to a property of a datetimelike object are not "
101
- "supported and are discarded. Change values on the original." )
114
+ result .is_copy = ("modifications to a property of a datetimelike "
115
+ "object are not supported and are discarded. "
116
+ "Change values on the original." )
102
117
103
118
return result
104
119
105
120
def _delegate_property_set (self , name , value , * args , ** kwargs ):
106
- raise ValueError ("modifications to a property of a datetimelike object are not "
107
- "supported. Change values on the original." )
121
+ raise ValueError ("modifications to a property of a datetimelike "
122
+ "object are not supported. Change values on the "
123
+ "original." )
108
124
109
125
def _delegate_method (self , name , * args , ** kwargs ):
110
126
from pandas import Series
@@ -118,8 +134,9 @@ def _delegate_method(self, name, *args, **kwargs):
118
134
result = Series (result , index = self .index , name = self .name )
119
135
120
136
# setting this object will show a SettingWithCopyWarning/Error
121
- result .is_copy = ("modifications to a method of a datetimelike object are not "
122
- "supported and are discarded. Change values on the original." )
137
+ result .is_copy = ("modifications to a method of a datetimelike object "
138
+ "are not supported and are discarded. Change "
139
+ "values on the original." )
123
140
124
141
return result
125
142
@@ -205,9 +222,10 @@ class PeriodProperties(Properties):
205
222
Raises TypeError if the Series does not contain datetimelike values.
206
223
"""
207
224
208
- PeriodProperties ._add_delegate_accessors (delegate = PeriodIndex ,
209
- accessors = PeriodIndex ._datetimelike_ops ,
210
- typ = 'property' )
225
+ PeriodProperties ._add_delegate_accessors (
226
+ delegate = PeriodIndex ,
227
+ accessors = PeriodIndex ._datetimelike_ops ,
228
+ typ = 'property' )
211
229
PeriodProperties ._add_delegate_accessors (delegate = PeriodIndex ,
212
230
accessors = ["strftime" ],
213
231
typ = 'method' )
@@ -222,8 +240,8 @@ class CombinedDatetimelikeProperties(DatetimeProperties, TimedeltaProperties):
222
240
223
241
def _concat_compat (to_concat , axis = 0 ):
224
242
"""
225
- provide concatenation of an datetimelike array of arrays each of which is a single
226
- M8[ns], datetimet64[ns, tz] or m8[ns] dtype
243
+ provide concatenation of an datetimelike array of arrays each of which is a
244
+ single M8[ns], datetimet64[ns, tz] or m8[ns] dtype
227
245
228
246
Parameters
229
247
----------
@@ -258,24 +276,26 @@ def convert_to_pydatetime(x, axis):
258
276
if 'datetimetz' in typs :
259
277
260
278
# we require ALL of the same tz for datetimetz
261
- tzs = set ([ getattr (x ,'tz' ,None ) for x in to_concat ]) - set ([None ])
279
+ tzs = set ([getattr (x , 'tz' , None ) for x in to_concat ]) - set ([None ])
262
280
if len (tzs ) == 1 :
263
- return DatetimeIndex (np .concatenate ([ x .tz_localize (None ).asi8 for x in to_concat ]), tz = list (tzs )[0 ])
281
+ return DatetimeIndex (np .concatenate ([x .tz_localize (None ).asi8
282
+ for x in to_concat ]),
283
+ tz = list (tzs )[0 ])
264
284
265
285
# single dtype
266
286
if len (typs ) == 1 :
267
287
268
- if not len (typs - set (['datetime' ])):
288
+ if not len (typs - set (['datetime' ])):
269
289
new_values = np .concatenate ([x .view (np .int64 ) for x in to_concat ],
270
290
axis = axis )
271
291
return new_values .view (_NS_DTYPE )
272
292
273
- elif not len (typs - set (['timedelta' ])):
293
+ elif not len (typs - set (['timedelta' ])):
274
294
new_values = np .concatenate ([x .view (np .int64 ) for x in to_concat ],
275
295
axis = axis )
276
296
return new_values .view (_TD_DTYPE )
277
297
278
298
# need to coerce to object
279
299
to_concat = [convert_to_pydatetime (x , axis ) for x in to_concat ]
280
300
281
- return np .concatenate (to_concat ,axis = axis )
301
+ return np .concatenate (to_concat , axis = axis )
0 commit comments