Skip to content

Commit 8961404

Browse files
Chang Shewesm
authored andcommitted
ENH: freqstr with offset #1184
1 parent 5d90f7d commit 8961404

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

pandas/tseries/offsets.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,11 @@ def freqstr(self):
221221
return repr(self)
222222

223223
if self.n != 1:
224-
return '%d%s' % (self.n, code)
224+
fstr = '%d%s' % (self.n, code)
225225
else:
226-
return code
226+
fstr = code
227227

228+
return fstr
228229

229230
class BusinessDay(CacheableOffset, DateOffset):
230231
"""
@@ -261,6 +262,55 @@ def __repr__(self):
261262
out += '>'
262263
return out
263264

265+
@property
266+
def freqstr(self):
267+
try:
268+
code = self.rule_code
269+
except NotImplementedError:
270+
return repr(self)
271+
272+
if self.n != 1:
273+
fstr = '%d%s' % (self.n, code)
274+
else:
275+
fstr = code
276+
277+
if self.offset:
278+
fstr += self._offset_str()
279+
280+
return fstr
281+
282+
def _offset_str(self):
283+
284+
def get_str(td):
285+
off_str = ''
286+
if td.days > 0:
287+
off_str += str(td.days) + 'D'
288+
if td.seconds > 0:
289+
s = td.seconds
290+
hrs = int(s / 3600)
291+
if hrs != 0:
292+
off_str += str(hrs) + 'H'
293+
s -= hrs * 3600
294+
mts = int(s / 60)
295+
if mts != 0:
296+
off_str += str(mts) + 'Min'
297+
s -= mts * 60
298+
if s != 0:
299+
off_str += str(s) + 's'
300+
if td.microseconds > 0:
301+
off_str += str(td.microseconds) + 'us'
302+
return off_str
303+
304+
if isinstance(self.offset, timedelta):
305+
tot_sec = self.offset.total_seconds()
306+
if tot_sec > 0:
307+
off_str = '+' + get_str(self.offset)
308+
if tot_sec < 0:
309+
off_str = '-' + get_str(-self.offset)
310+
return off_str
311+
else:
312+
return '+' + repr(self.offset)
313+
264314
def isAnchored(self):
265315
return (self.n == 1)
266316

pandas/tseries/tests/test_offsets.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,13 @@ def test_dateoffset_misc():
13891389

13901390
assert(not offsets.DateOffset(months=2) == 2)
13911391

1392+
def test_freq_offsets():
1393+
off = BDay(1, offset=timedelta(0, 1800))
1394+
assert(off.freqstr == 'B+30Min')
1395+
1396+
off = BDay(1, offset=timedelta(0, -1800))
1397+
assert(off.freqstr == 'B-30Min')
1398+
13921399
if __name__ == '__main__':
13931400
import nose
13941401
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)