Skip to content

Commit 28cee84

Browse files
committed
CLN: fix all flake8 warnings (except pandas/tseries/resample.py) in pandas/tseries
1 parent c4b0a22 commit 28cee84

30 files changed

+5200
-4093
lines changed

pandas/tseries/api.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
"""
44

5+
# flake8: noqa
56

67
from pandas.tseries.index import DatetimeIndex, date_range, bdate_range
78
from pandas.tseries.frequencies import infer_freq

pandas/tseries/base.py

+50-36
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,28 @@
1717
import pandas.algos as _algos
1818

1919

20-
2120
class DatelikeOps(object):
2221
""" common ops for DatetimeIndex/PeriodIndex, but not TimedeltaIndex """
2322

2423
def strftime(self, date_format):
25-
"""
26-
Return an array of formatted strings specified by date_format, which
27-
supports the same string format as the python standard library. Details
28-
of the string format can be found in the `python string format doc
29-
<https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior>`__
24+
return np.asarray(self.format(date_format=date_format))
25+
strftime.__doc__ = """
26+
Return an array of formatted strings specified by date_format, which
27+
supports the same string format as the python standard library. Details
28+
of the string format can be found in `python string format doc <{0}>`__
3029
31-
.. versionadded:: 0.17.0
30+
.. versionadded:: 0.17.0
3231
33-
Parameters
34-
----------
35-
date_format : str
36-
date format string (e.g. "%Y-%m-%d")
32+
Parameters
33+
----------
34+
date_format : str
35+
date format string (e.g. "%Y-%m-%d")
3736
38-
Returns
39-
-------
40-
ndarray of formatted strings
41-
"""
42-
return np.asarray(self.format(date_format=date_format))
37+
Returns
38+
-------
39+
ndarray of formatted strings
40+
""".format("https://docs.python.org/2/library/datetime.html"
41+
"#strftime-and-strptime-behavior")
4342

4443

4544
class TimelikeOps(object):
@@ -68,7 +67,7 @@ def _round(self, freq, rounder):
6867
unit = to_offset(freq).nanos
6968

7069
# round the local times
71-
if getattr(self,'tz',None) is not None:
70+
if getattr(self, 'tz', None) is not None:
7271
values = self.tz_localize(None).asi8
7372
else:
7473
values = self.asi8
@@ -81,7 +80,7 @@ def _round(self, freq, rounder):
8180
result = self._shallow_copy(result, **attribs)
8281

8382
# reconvert to local tz
84-
if getattr(self,'tz',None) is not None:
83+
if getattr(self, 'tz', None) is not None:
8584
result = result.tz_localize(self.tz)
8685
return result
8786

@@ -181,7 +180,9 @@ def __getitem__(self, key):
181180

182181
@property
183182
def freqstr(self):
184-
""" return the frequency object as a string if its set, otherwise None """
183+
"""
184+
Return the frequency object as a string if its set, otherwise None
185+
"""
185186
if self.freq is None:
186187
return None
187188
return self.freq.freqstr
@@ -291,7 +292,8 @@ def _maybe_mask_results(self, result, fill_value=None, convert=None):
291292
-------
292293
result : ndarray with values replace by the fill_value
293294
294-
mask the result if needed, convert to the provided dtype if its not None
295+
mask the result if needed, convert to the provided dtype if its not
296+
None
295297
296298
This is an internal routine
297299
"""
@@ -408,7 +410,7 @@ def _format_attrs(self):
408410
freq = self.freqstr
409411
if freq is not None:
410412
freq = "'%s'" % freq
411-
attrs.append(('freq',freq))
413+
attrs.append(('freq', freq))
412414
return attrs
413415

414416
@cache_readonly
@@ -424,18 +426,21 @@ def resolution(self):
424426

425427
def _convert_scalar_indexer(self, key, kind=None):
426428
"""
427-
we don't allow integer or float indexing on datetime-like when using loc
429+
we don't allow integer or float indexing on datetime-like when using
430+
loc
428431
429432
Parameters
430433
----------
431434
key : label of the slice bound
432435
kind : optional, type of the indexing operation (loc/ix/iloc/None)
433436
"""
434437

435-
if kind in ['loc'] and lib.isscalar(key) and (is_integer(key) or is_float(key)):
436-
self._invalid_indexer('index',key)
438+
if (kind in ['loc'] and lib.isscalar(key) and
439+
(is_integer(key) or is_float(key))):
440+
self._invalid_indexer('index', key)
437441

438-
return super(DatetimeIndexOpsMixin, self)._convert_scalar_indexer(key, kind=kind)
442+
return (super(DatetimeIndexOpsMixin, self)
443+
._convert_scalar_indexer(key, kind=kind))
439444

440445
def _add_datelike(self, other):
441446
raise AbstractMethodError(self)
@@ -445,7 +450,10 @@ def _sub_datelike(self, other):
445450

446451
@classmethod
447452
def _add_datetimelike_methods(cls):
448-
""" add in the datetimelike methods (as we may have to override the superclass) """
453+
"""
454+
add in the datetimelike methods (as we may have to override the
455+
superclass)
456+
"""
449457

450458
def __add__(self, other):
451459
from pandas.core.index import Index
@@ -454,14 +462,17 @@ def __add__(self, other):
454462
if isinstance(other, TimedeltaIndex):
455463
return self._add_delta(other)
456464
elif isinstance(self, TimedeltaIndex) and isinstance(other, Index):
457-
if hasattr(other,'_add_delta'):
465+
if hasattr(other, '_add_delta'):
458466
return other._add_delta(self)
459-
raise TypeError("cannot add TimedeltaIndex and {typ}".format(typ=type(other)))
467+
raise TypeError("cannot add TimedeltaIndex and {typ}"
468+
.format(typ=type(other)))
460469
elif isinstance(other, Index):
461-
warnings.warn("using '+' to provide set union with datetimelike Indexes is deprecated, "
462-
"use .union()",FutureWarning, stacklevel=2)
470+
warnings.warn("using '+' to provide set union with "
471+
"datetimelike Indexes is deprecated, "
472+
"use .union()", FutureWarning, stacklevel=2)
463473
return self.union(other)
464-
elif isinstance(other, (DateOffset, timedelta, np.timedelta64, tslib.Timedelta)):
474+
elif isinstance(other, (DateOffset, timedelta, np.timedelta64,
475+
tslib.Timedelta)):
465476
return self._add_delta(other)
466477
elif com.is_integer(other):
467478
return self.shift(other)
@@ -480,13 +491,16 @@ def __sub__(self, other):
480491
return self._add_delta(-other)
481492
elif isinstance(self, TimedeltaIndex) and isinstance(other, Index):
482493
if not isinstance(other, TimedeltaIndex):
483-
raise TypeError("cannot subtract TimedeltaIndex and {typ}".format(typ=type(other)))
494+
raise TypeError("cannot subtract TimedeltaIndex and {typ}"
495+
.format(typ=type(other)))
484496
return self._add_delta(-other)
485497
elif isinstance(other, Index):
486-
warnings.warn("using '-' to provide set differences with datetimelike Indexes is deprecated, "
487-
"use .difference()",FutureWarning, stacklevel=2)
498+
warnings.warn("using '-' to provide set differences with "
499+
"datetimelike Indexes is deprecated, "
500+
"use .difference()", FutureWarning, stacklevel=2)
488501
return self.difference(other)
489-
elif isinstance(other, (DateOffset, timedelta, np.timedelta64, tslib.Timedelta)):
502+
elif isinstance(other, (DateOffset, timedelta, np.timedelta64,
503+
tslib.Timedelta)):
490504
return self._add_delta(-other)
491505
elif com.is_integer(other):
492506
return self.shift(-other)
@@ -630,5 +644,5 @@ def summary(self, name=None):
630644
result += '\nFreq: %s' % self.freqstr
631645

632646
# display as values, not quoted
633-
result = result.replace("'","")
647+
result = result.replace("'", "")
634648
return result

pandas/tseries/common.py

+48-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
## datetimelike delegation ##
1+
"""
2+
datetimelike delegation
3+
"""
24

35
import numpy as np
46
from pandas.core.base import PandasDelegate, NoNewAttributesMixin
@@ -8,20 +10,25 @@
810
from pandas.tseries.tdi import TimedeltaIndex
911
from pandas import tslib
1012
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,
1215
is_datetime64_dtype, is_datetime64tz_dtype,
1316
is_timedelta64_dtype, is_categorical_dtype,
1417
get_dtype_kinds, take_1d)
1518

19+
1620
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+
"""
1824
try:
1925
maybe_to_datetimelike(data)
2026
return True
2127
except (Exception):
2228
pass
2329
return False
2430

31+
2532
def maybe_to_datetimelike(data, copy=False):
2633
"""
2734
return a DelegatedClass of a Series that is datetimelike
@@ -42,7 +49,8 @@ def maybe_to_datetimelike(data, copy=False):
4249
from pandas import Series
4350

4451
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)))
4654

4755
index = data.index
4856
name = data.name
@@ -51,22 +59,28 @@ def maybe_to_datetimelike(data, copy=False):
5159
data = orig.values.categories
5260

5361
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)
5664
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'),
5867
index, data.name, orig=orig)
5968
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,
6171
name=name, orig=orig)
6272
else:
6373
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)
6576
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,
6779
name=name, orig=orig)
6880

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+
7084

7185
class Properties(PandasDelegate, NoNewAttributesMixin):
7286

@@ -80,7 +94,7 @@ def __init__(self, values, index, name, orig=None):
8094
def _delegate_property_get(self, name):
8195
from pandas import Series
8296

83-
result = getattr(self.values,name)
97+
result = getattr(self.values, name)
8498

8599
# maybe need to upcast (ints)
86100
if isinstance(result, np.ndarray):
@@ -97,14 +111,16 @@ def _delegate_property_get(self, name):
97111
result = Series(result, index=self.index, name=self.name)
98112

99113
# 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.")
102117

103118
return result
104119

105120
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.")
108124

109125
def _delegate_method(self, name, *args, **kwargs):
110126
from pandas import Series
@@ -118,8 +134,9 @@ def _delegate_method(self, name, *args, **kwargs):
118134
result = Series(result, index=self.index, name=self.name)
119135

120136
# 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.")
123140

124141
return result
125142

@@ -205,9 +222,10 @@ class PeriodProperties(Properties):
205222
Raises TypeError if the Series does not contain datetimelike values.
206223
"""
207224

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')
211229
PeriodProperties._add_delegate_accessors(delegate=PeriodIndex,
212230
accessors=["strftime"],
213231
typ='method')
@@ -222,8 +240,8 @@ class CombinedDatetimelikeProperties(DatetimeProperties, TimedeltaProperties):
222240

223241
def _concat_compat(to_concat, axis=0):
224242
"""
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
227245
228246
Parameters
229247
----------
@@ -258,24 +276,26 @@ def convert_to_pydatetime(x, axis):
258276
if 'datetimetz' in typs:
259277

260278
# 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])
262280
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])
264284

265285
# single dtype
266286
if len(typs) == 1:
267287

268-
if not len(typs-set(['datetime'])):
288+
if not len(typs - set(['datetime'])):
269289
new_values = np.concatenate([x.view(np.int64) for x in to_concat],
270290
axis=axis)
271291
return new_values.view(_NS_DTYPE)
272292

273-
elif not len(typs-set(['timedelta'])):
293+
elif not len(typs - set(['timedelta'])):
274294
new_values = np.concatenate([x.view(np.int64) for x in to_concat],
275295
axis=axis)
276296
return new_values.view(_TD_DTYPE)
277297

278298
# need to coerce to object
279299
to_concat = [convert_to_pydatetime(x, axis) for x in to_concat]
280300

281-
return np.concatenate(to_concat,axis=axis)
301+
return np.concatenate(to_concat, axis=axis)

0 commit comments

Comments
 (0)