Skip to content

Commit d8203cb

Browse files
committed
PEP cleanup
1 parent 4437e9c commit d8203cb

File tree

7 files changed

+166
-73
lines changed

7 files changed

+166
-73
lines changed

pandas/tests/test_categorical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3763,7 +3763,7 @@ def test_dt_accessor_api_for_categorical(self):
37633763
('round', ("D",), {}),
37643764
('floor', ("D",), {}),
37653765
('ceil', ("D",), {}),
3766-
#('tz_localize', ("UTC",), {}),
3766+
# ('tz_localize', ("UTC",), {}),
37673767
]
37683768
_special_func_names = [f[0] for f in special_func_defs]
37693769

pandas/tests/test_series.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,25 @@ def test_dt_namespace_accessor(self):
8484
# GH 7207
8585
# test .dt namespace accessor
8686

87-
ok_for_base = ['year','month','day','hour','minute','second','weekofyear','week','dayofweek','weekday','dayofyear','quarter','freq','days_in_month','daysinmonth']
87+
ok_for_base = ['year', 'month', 'day', 'hour', 'minute', 'second',
88+
'weekofyear', 'week', 'dayofweek', 'weekday',
89+
'dayofyear', 'quarter', 'freq', 'days_in_month',
90+
'daysinmonth']
8891
ok_for_period = ok_for_base + ['qyear']
8992
ok_for_period_methods = ['strftime']
90-
ok_for_dt = ok_for_base + ['date','time','microsecond','nanosecond', 'is_month_start', 'is_month_end', 'is_quarter_start',
91-
'is_quarter_end', 'is_year_start', 'is_year_end', 'tz']
92-
ok_for_dt_methods = ['to_period','to_pydatetime','tz_localize','tz_convert', 'normalize', 'strftime', 'round', 'floor', 'ceil']
93-
ok_for_td = ['days','seconds','microseconds','nanoseconds']
94-
ok_for_td_methods = ['components','to_pytimedelta','total_seconds','round', 'floor', 'ceil']
93+
ok_for_dt = ok_for_base + ['date', 'time', 'microsecond', 'nanosecond',
94+
'is_month_start', 'is_month_end',
95+
'is_quarter_start', 'is_quarter_end',
96+
'is_year_start', 'is_year_end', 'tz']
97+
ok_for_dt_methods = ['to_period', 'to_pydatetime', 'tz_localize',
98+
'tz_convert', 'normalize', 'strftime', 'round',
99+
'floor', 'ceil']
100+
ok_for_td = ['days', 'seconds', 'microseconds', 'nanoseconds']
101+
ok_for_td_methods = ['components', 'to_pytimedelta', 'total_seconds',
102+
'round', 'floor', 'ceil']
95103

96104
def get_expected(s, name):
97-
result = getattr(Index(s._values),prop)
105+
result = getattr(Index(s._values), prop)
98106
if isinstance(result, np.ndarray):
99107
if com.is_integer_dtype(result):
100108
result = result.astype('int64')
@@ -141,26 +149,42 @@ def compare(s, name):
141149
tm.assert_series_equal(result, expected)
142150

143151
# round
144-
s = Series(pd.to_datetime(['2012-01-01 13:00:00', '2012-01-01 12:01:00', '2012-01-01 08:00:00']))
152+
s = Series(pd.to_datetime(['2012-01-01 13:00:00',
153+
'2012-01-01 12:01:00',
154+
'2012-01-01 08:00:00']))
145155
result = s.dt.round('D')
146-
expected = Series(pd.to_datetime(['2012-01-02', '2012-01-02', '2012-01-01']))
156+
expected = Series(pd.to_datetime(['2012-01-02',
157+
'2012-01-02',
158+
'2012-01-01']))
147159
tm.assert_series_equal(result, expected)
148160

149161
# round with tz
150-
result = s.dt.tz_localize('UTC').dt.tz_convert('US/Eastern').dt.round('D')
151-
expected = Series(pd.to_datetime(['2012-01-01', '2012-01-01', '2012-01-01']).tz_localize('US/Eastern'))
162+
result = s.dt.tz_localize('UTC').dt.tz_convert(
163+
'US/Eastern').dt.round('D')
164+
expected = Series(pd.to_datetime(['2012-01-01',
165+
'2012-01-01',
166+
'2012-01-01']
167+
).tz_localize('US/Eastern'))
152168
tm.assert_series_equal(result, expected)
153169

154170
# floor
155-
s = Series(pd.to_datetime(['2012-01-01 13:00:00', '2012-01-01 12:01:00', '2012-01-01 08:00:00']))
171+
s = Series(pd.to_datetime(['2012-01-01 13:00:00',
172+
'2012-01-01 12:01:00',
173+
'2012-01-01 08:00:00']))
156174
result = s.dt.floor('D')
157-
expected = Series(pd.to_datetime(['2012-01-01', '2012-01-01', '2012-01-01']))
175+
expected = Series(pd.to_datetime(['2012-01-01',
176+
'2012-01-01',
177+
'2012-01-01']))
158178
tm.assert_series_equal(result, expected)
159179

160180
# ceil
161-
s = Series(pd.to_datetime(['2012-01-01 13:00:00', '2012-01-01 12:01:00', '2012-01-01 08:00:00']))
181+
s = Series(pd.to_datetime(['2012-01-01 13:00:00',
182+
'2012-01-01 12:01:00',
183+
'2012-01-01 08:00:00']))
162184
result = s.dt.ceil('D')
163-
expected = Series(pd.to_datetime(['2012-01-02', '2012-01-02', '2012-01-02']))
185+
expected = Series(pd.to_datetime(['2012-01-02',
186+
'2012-01-02',
187+
'2012-01-02']))
164188
tm.assert_series_equal(result, expected)
165189

166190
# datetimeindex with tz

pandas/tseries/base.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def strftime(self, date_format):
4141
"""
4242
return np.asarray(self.format(date_format=date_format))
4343

44+
4445
class TimelikeOps(object):
4546
""" common ops for TimedeltaIndex/DatetimeIndex, but not PeriodIndex """
4647

@@ -60,6 +61,7 @@ class TimelikeOps(object):
6061
------
6162
ValueError if the freq cannot be converted
6263
""")
64+
6365
def _round(self, freq, rounder):
6466

6567
from pandas.tseries.frequencies import to_offset
@@ -70,7 +72,7 @@ def _round(self, freq, rounder):
7072
values = self.tz_localize(None).asi8
7173
else:
7274
values = self.asi8
73-
result = (unit*rounder(values/float(unit))).astype('i8')
75+
result = (unit * rounder(values / float(unit))).astype('i8')
7476
attribs = self._get_attributes_dict()
7577
if 'freq' in attribs:
7678
attribs['freq'] = None
@@ -82,18 +84,19 @@ def _round(self, freq, rounder):
8284
if getattr(self,'tz',None) is not None:
8385
result = result.tz_localize(self.tz)
8486
return result
85-
87+
8688
@Appender(_round_doc % "round")
8789
def round(self, freq):
88-
return self._round(freq, np.round)
90+
return self._round(freq, np.round)
8991

9092
@Appender(_round_doc % "floor")
9193
def floor(self, freq):
92-
return self._round(freq, np.floor)
94+
return self._round(freq, np.floor)
9395

9496
@Appender(_round_doc % "floor")
9597
def ceil(self, freq):
96-
return self._round(freq, np.ceil)
98+
return self._round(freq, np.ceil)
99+
97100

98101
class DatetimeIndexOpsMixin(object):
99102
""" common ops mixin to support a unified inteface datetimelike Index """

pandas/tseries/common.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,16 @@ class DatetimeProperties(Properties):
141141
def to_pydatetime(self):
142142
return self.values.to_pydatetime()
143143

144-
DatetimeProperties._add_delegate_accessors(delegate=DatetimeIndex,
145-
accessors=DatetimeIndex._datetimelike_ops,
146-
typ='property')
147-
DatetimeProperties._add_delegate_accessors(delegate=DatetimeIndex,
148-
accessors=["to_period","tz_localize","tz_convert",
149-
"normalize","strftime","round", "floor", "ceil"],
150-
typ='method')
144+
DatetimeProperties._add_delegate_accessors(
145+
delegate=DatetimeIndex,
146+
accessors=DatetimeIndex._datetimelike_ops,
147+
typ='property')
148+
DatetimeProperties._add_delegate_accessors(
149+
delegate=DatetimeIndex,
150+
accessors=["to_period", "tz_localize", "tz_convert",
151+
"normalize", "strftime", "round", "floor", "ceil"],
152+
typ='method')
153+
151154

152155
class TimedeltaProperties(Properties):
153156
"""
@@ -178,12 +181,15 @@ def components(self):
178181
"""
179182
return self.values.components.set_index(self.index)
180183

181-
TimedeltaProperties._add_delegate_accessors(delegate=TimedeltaIndex,
182-
accessors=TimedeltaIndex._datetimelike_ops,
183-
typ='property')
184-
TimedeltaProperties._add_delegate_accessors(delegate=TimedeltaIndex,
185-
accessors=["to_pytimedelta", "total_seconds", "round", "floor", "ceil"],
186-
typ='method')
184+
TimedeltaProperties._add_delegate_accessors(
185+
delegate=TimedeltaIndex,
186+
accessors=TimedeltaIndex._datetimelike_ops,
187+
typ='property')
188+
TimedeltaProperties._add_delegate_accessors(
189+
delegate=TimedeltaIndex,
190+
accessors=["to_pytimedelta", "total_seconds", "round", "floor", "ceil"],
191+
typ='method')
192+
187193

188194
class PeriodProperties(Properties):
189195
"""

pandas/tseries/index.py

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,23 +1469,61 @@ def _set_freq(self, value):
14691469
hour = _field_accessor('hour', 'h', "The hours of the datetime")
14701470
minute = _field_accessor('minute', 'm', "The minutes of the datetime")
14711471
second = _field_accessor('second', 's', "The seconds of the datetime")
1472-
microsecond = _field_accessor('microsecond', 'us', "The microseconds of the datetime")
1473-
nanosecond = _field_accessor('nanosecond', 'ns', "The nanoseconds of the datetime")
1474-
weekofyear = _field_accessor('weekofyear', 'woy', "The week ordinal of the year")
1472+
microsecond = _field_accessor(
1473+
'microsecond',
1474+
'us',
1475+
"The microseconds of the datetime")
1476+
nanosecond = _field_accessor(
1477+
'nanosecond',
1478+
'ns',
1479+
"The nanoseconds of the datetime")
1480+
weekofyear = _field_accessor(
1481+
'weekofyear',
1482+
'woy',
1483+
"The week ordinal of the year")
14751484
week = weekofyear
1476-
dayofweek = _field_accessor('dayofweek', 'dow',
1477-
"The day of the week with Monday=0, Sunday=6")
1485+
dayofweek = _field_accessor(
1486+
'dayofweek',
1487+
'dow',
1488+
"The day of the week with Monday=0, Sunday=6")
14781489
weekday = dayofweek
1479-
dayofyear = _field_accessor('dayofyear', 'doy', "The ordinal day of the year")
1480-
quarter = _field_accessor('quarter', 'q', "The quarter of the date")
1481-
days_in_month = _field_accessor('days_in_month', 'dim', "The number of days in the month\n\n.. versionadded:: 0.16.0")
1490+
dayofyear = _field_accessor(
1491+
'dayofyear',
1492+
'doy',
1493+
"The ordinal day of the year")
1494+
quarter = _field_accessor(
1495+
'quarter',
1496+
'q',
1497+
"The quarter of the date")
1498+
days_in_month = _field_accessor(
1499+
'days_in_month',
1500+
'dim',
1501+
"The number of days in the month\n\n.. versionadded:: 0.16.0")
14821502
daysinmonth = days_in_month
1483-
is_month_start = _field_accessor('is_month_start', 'is_month_start', "Logical indicating if first day of month (defined by frequency)")
1484-
is_month_end = _field_accessor('is_month_end', 'is_month_end', "Logical indicating if last day of month (defined by frequency)")
1485-
is_quarter_start = _field_accessor('is_quarter_start', 'is_quarter_start', "Logical indicating if first day of quarter (defined by frequency)")
1486-
is_quarter_end = _field_accessor('is_quarter_end', 'is_quarter_end', "Logical indicating if last day of quarter (defined by frequency)")
1487-
is_year_start = _field_accessor('is_year_start', 'is_year_start', "Logical indicating if first day of year (defined by frequency)")
1488-
is_year_end = _field_accessor('is_year_end', 'is_year_end', "Logical indicating if last day of year (defined by frequency)")
1503+
is_month_start = _field_accessor(
1504+
'is_month_start',
1505+
'is_month_start',
1506+
"Logical indicating if first day of month (defined by frequency)")
1507+
is_month_end = _field_accessor(
1508+
'is_month_end',
1509+
'is_month_end',
1510+
"Logical indicating if last day of month (defined by frequency)")
1511+
is_quarter_start = _field_accessor(
1512+
'is_quarter_start',
1513+
'is_quarter_start',
1514+
"Logical indicating if first day of quarter (defined by frequency)")
1515+
is_quarter_end = _field_accessor(
1516+
'is_quarter_end',
1517+
'is_quarter_end',
1518+
"Logical indicating if last day of quarter (defined by frequency)")
1519+
is_year_start = _field_accessor(
1520+
'is_year_start',
1521+
'is_year_start',
1522+
"Logical indicating if first day of year (defined by frequency)")
1523+
is_year_end = _field_accessor(
1524+
'is_year_end',
1525+
'is_year_end',
1526+
"Logical indicating if last day of year (defined by frequency)")
14891527

14901528
@property
14911529
def time(self):

pandas/tseries/tests/test_timedeltas.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -225,35 +225,57 @@ def test_round(self):
225225
self.assertEqual(r2, s2)
226226

227227
# invalid
228-
for freq in ['Y','M','foobar']:
229-
self.assertRaises(ValueError, lambda : t1.round(freq))
228+
for freq in ['Y', 'M', 'foobar']:
229+
self.assertRaises(ValueError, lambda: t1.round(freq))
230230

231-
t1 = timedelta_range('1 days',periods=3,freq='1 min 2 s 3 us')
232-
t2 = -1*t1
233-
t1a = timedelta_range('1 days',periods=3,freq='1 min 2 s')
234-
t1b = timedelta_range('1 days',periods=3,freq='1 min')
235-
t1c = pd.TimedeltaIndex([1,1,1],unit='D')
231+
t1 = timedelta_range('1 days', periods=3, freq='1 min 2 s 3 us')
232+
t2 = -1 * t1
233+
t1a = timedelta_range('1 days', periods=3, freq='1 min 2 s')
234+
t1c = pd.TimedeltaIndex([1, 1, 1], unit='D')
236235

237236
# note that negative times round DOWN! so don't give whole numbers
238237
for (freq, s1, s2) in [('N', t1, t2),
239238
('U', t1, t2),
240-
('L', t1a, TimedeltaIndex(['-1 days +00:00:00', '-2 days +23:58:58', '-2 days +23:57:56'],
241-
dtype='timedelta64[ns]', freq=None)),
242-
('S', t1a, TimedeltaIndex(['-1 days +00:00:00', '-2 days +23:58:58', '-2 days +23:57:56'],
243-
dtype='timedelta64[ns]', freq=None)),
244-
('12T', t1c, TimedeltaIndex(['-1 days', '-1 days', '-1 days'],
245-
dtype='timedelta64[ns]', freq=None)),
246-
('H', t1c, TimedeltaIndex(['-1 days', '-1 days', '-1 days'],
247-
dtype='timedelta64[ns]', freq=None)),
248-
('d', t1c, pd.TimedeltaIndex([-1,-1,-1],unit='D'))]:
239+
('L', t1a,
240+
TimedeltaIndex(['-1 days +00:00:00',
241+
'-2 days +23:58:58',
242+
'-2 days +23:57:56'],
243+
dtype='timedelta64[ns]',
244+
freq=None)
245+
),
246+
('S', t1a,
247+
TimedeltaIndex(['-1 days +00:00:00',
248+
'-2 days +23:58:58',
249+
'-2 days +23:57:56'],
250+
dtype='timedelta64[ns]',
251+
freq=None)
252+
),
253+
('12T', t1c,
254+
TimedeltaIndex(['-1 days',
255+
'-1 days',
256+
'-1 days'],
257+
dtype='timedelta64[ns]',
258+
freq=None)
259+
),
260+
('H', t1c,
261+
TimedeltaIndex(['-1 days',
262+
'-1 days',
263+
'-1 days'],
264+
dtype='timedelta64[ns]',
265+
freq=None)
266+
),
267+
('d', t1c,
268+
pd.TimedeltaIndex([-1, -1, -1], unit='D')
269+
)]:
270+
249271
r1 = t1.round(freq)
250272
tm.assert_index_equal(r1, s1)
251273
r2 = t2.round(freq)
252-
tm.assert_index_equal(r2, s2)
274+
tm.assert_index_equal(r2, s2)
253275

254276
# invalid
255-
for freq in ['Y','M','foobar']:
256-
self.assertRaises(ValueError, lambda : t1.round(freq))
277+
for freq in ['Y', 'M', 'foobar']:
278+
self.assertRaises(ValueError, lambda: t1.round(freq))
257279

258280
def test_repr(self):
259281

pandas/tseries/tests/test_timeseries.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def test_indexing(self):
246246
expected = ts['2001']
247247
expected.name = 'A'
248248

249-
df.loc['2001','A'] = 1
249+
df.loc['2001', 'A'] = 1
250250

251251
result = df['2001']['A']
252252
assert_series_equal(expected, result)
@@ -2805,9 +2805,9 @@ def test_round(self):
28052805
expected = Timestamp('20130104 12:30:00')
28062806
self.assertEqual(result, expected)
28072807

2808-
dti = date_range('20130101 09:10:11',periods=5)
2808+
dti = date_range('20130101 09:10:11', periods=5)
28092809
result = dti.round('D')
2810-
expected = date_range('20130101',periods=5)
2810+
expected = date_range('20130101', periods=5)
28112811
tm.assert_index_equal(result, expected)
28122812

28132813
# floor
@@ -2823,12 +2823,12 @@ def test_round(self):
28232823
self.assertEqual(result, expected)
28242824

28252825
# round with tz
2826-
dt = Timestamp('20130101 09:10:11',tz='US/Eastern')
2826+
dt = Timestamp('20130101 09:10:11', tz='US/Eastern')
28272827
result = dt.round('D')
2828-
expected = Timestamp('20130101',tz='US/Eastern')
2828+
expected = Timestamp('20130101', tz='US/Eastern')
28292829
self.assertEqual(result, expected)
28302830

2831-
dt = Timestamp('20130101 09:10:11',tz='US/Eastern')
2831+
dt = Timestamp('20130101 09:10:11', tz='US/Eastern')
28322832
result = dt.round('s')
28332833
self.assertEqual(result, dt)
28342834

0 commit comments

Comments
 (0)