Skip to content

Commit ec221c6

Browse files
committed
BUG: fix NumPy 1.7 argmin workaround, test coverage
1 parent de8f14d commit ec221c6

File tree

8 files changed

+60
-38
lines changed

8 files changed

+60
-38
lines changed

pandas/core/internals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ def insert(self, loc, item, value):
898898
# new block
899899
self._add_new_block(item, value, loc=loc)
900900

901-
if len(self.blocks) > 20:
901+
if len(self.blocks) > 100:
902902
self._consolidate_inplace()
903903

904904
def set_items_norename(self, value):

pandas/tseries/frequencies.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,11 @@ def infer_freq(index, warn=True):
694694
freq : string or None
695695
None if no discernable frequency
696696
"""
697+
from pandas.tseries.index import DatetimeIndex
698+
699+
if not isinstance(index, DatetimeIndex):
700+
index = DatetimeIndex(index)
701+
697702
inferer = _FrequencyInferer(index, warn=warn)
698703
return inferer.get_freq()
699704

@@ -710,11 +715,6 @@ class _FrequencyInferer(object):
710715
"""
711716

712717
def __init__(self, index, warn=True):
713-
from pandas.tseries.index import DatetimeIndex
714-
715-
if not isinstance(index, DatetimeIndex):
716-
index = DatetimeIndex(index)
717-
718718
self.index = index
719719
self.values = np.asarray(index).view('i8')
720720
self.warn = warn
@@ -967,8 +967,6 @@ def is_superperiod(source, target):
967967
return target in ['D', 'B', 'H', 'T', 'S']
968968

969969
def _get_rule_month(source, default='DEC'):
970-
if isinstance(source, offsets.DateOffset):
971-
source = source.rule_code
972970
source = source.upper()
973971
if '-' not in source:
974972
return default

pandas/tseries/index.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1036,9 +1036,12 @@ def searchsorted(self, key, side='left'):
10361036
def is_type_compatible(self, typ):
10371037
return typ == self.inferred_type or typ == 'datetime'
10381038

1039-
# hack to workaround argmin failure
10401039
def argmin(self):
1041-
return (-self).argmax()
1040+
# hack to workaround argmin failure
1041+
try:
1042+
return self.values.argmin()
1043+
except Exception: # pragma: no cover
1044+
return self.asi8.argmin()
10421045

10431046
@property
10441047
def inferred_type(self):

pandas/tseries/offsets.py

+5-17
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ def rollback(self, someDate):
194194

195195
def rollforward(self, dt):
196196
"""Roll provided date forward to next offset only if not on offset"""
197-
if isinstance(dt, np.datetime64):
198-
dt = Timestamp(dt)
199197
if not self.onOffset(dt):
200198
dt = dt + self.__class__(1, **self.kwds)
201199
return dt
@@ -297,8 +295,6 @@ def apply(self, other):
297295
'datetime or timedelta!')
298296
@classmethod
299297
def onOffset(cls, dt):
300-
if isinstance(dt, np.datetime64):
301-
dt = Timestamp(dt)
302298
return dt.weekday() < 5
303299

304300

@@ -636,8 +632,6 @@ def apply(self, other):
636632
first = _get_firstbday(wkday)
637633

638634
monthsSince = (other.month - self.startingMonth) % 3
639-
if monthsSince == 3: # on offset
640-
monthsSince = 0
641635

642636
if n <= 0 and monthsSince != 0: # make sure to roll forward so negate
643637
monthsSince = monthsSince - 3
@@ -728,9 +722,6 @@ def apply(self, other):
728722

729723
monthsSince = (other.month - self.startingMonth) % 3
730724

731-
if monthsSince == 3: # on an offset
732-
monthsSince = 0
733-
734725
if n <= 0 and monthsSince != 0:
735726
# make sure you roll forward, so negate
736727
monthsSince = monthsSince - 3
@@ -756,7 +747,7 @@ def __init__(self, n=1, **kwds):
756747
self.month = kwds.get('month', 12)
757748

758749
if self.month < 1 or self.month > 12:
759-
raise Exception('Month must go from 1 to 12')
750+
raise ValueError('Month must go from 1 to 12')
760751

761752
DateOffset.__init__(self, n=n, **kwds)
762753

@@ -803,7 +794,7 @@ def __init__(self, n=1, **kwds):
803794
self.month = kwds.get('month', 1)
804795

805796
if self.month < 1 or self.month > 12:
806-
raise Exception('Month must go from 1 to 12')
797+
raise ValueError('Month must go from 1 to 12')
807798

808799
DateOffset.__init__(self, n=n, **kwds)
809800

@@ -845,7 +836,7 @@ def __init__(self, n=1, **kwds):
845836
self.month = kwds.get('month', 12)
846837

847838
if self.month < 1 or self.month > 12:
848-
raise Exception('Month must go from 1 to 12')
839+
raise ValueError('Month must go from 1 to 12')
849840

850841
DateOffset.__init__(self, n=n, **kwds)
851842

@@ -911,7 +902,7 @@ def __init__(self, n=1, **kwds):
911902
self.month = kwds.get('month', 12)
912903

913904
if self.month < 1 or self.month > 12:
914-
raise Exception('Month must go from 1 to 12')
905+
raise ValueError('Month must go from 1 to 12')
915906

916907
DateOffset.__init__(self, n=n, **kwds)
917908

@@ -1012,7 +1003,7 @@ def _delta_to_tick(delta):
10121003
return Milli(nanos // 1000000)
10131004
elif nanos % 1000 == 0:
10141005
return Micro(nanos // 1000)
1015-
else:
1006+
else: # pragma: no cover
10161007
return Nano(nanos)
10171008

10181009
def _delta_to_nanoseconds(delta):
@@ -1114,9 +1105,6 @@ def generate_range(start=None, end=None, periods=None,
11141105
end = None
11151106
periods = 0
11161107

1117-
if _count_not_none(start, end, periods) < 2:
1118-
raise ValueError('Must specify 2 of start, end, periods')
1119-
11201108
if end is None:
11211109
end = start + (periods - 1) * offset
11221110

pandas/tseries/tests/test_frequencies.py

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from pandas.tseries.frequencies import to_offset, infer_freq
1313
from pandas.tseries.tools import to_datetime
14+
import pandas.tseries.frequencies as fmod
1415
import pandas.tseries.offsets as offsets
1516

1617
import pandas.lib as lib
@@ -186,9 +187,21 @@ def test_not_monotonic(self):
186187
rng = rng[::-1]
187188
self.assert_(rng.inferred_freq is None)
188189

190+
def test_non_datetimeindex(self):
191+
rng = _dti(['1/31/2000', '1/31/2001', '1/31/2002'])
192+
193+
vals = rng.to_pydatetime()
194+
195+
result = infer_freq(vals)
196+
self.assertEqual(result, rng.inferred_freq)
197+
189198
MONTHS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP',
190199
'OCT', 'NOV', 'DEC']
191200

201+
def test_is_superperiod_subperiod():
202+
assert(fmod.is_superperiod(offsets.YearEnd(), offsets.MonthEnd()))
203+
assert(fmod.is_subperiod(offsets.MonthEnd(), offsets.YearEnd()))
204+
192205
if __name__ == '__main__':
193206
import nose
194207
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],

pandas/tseries/tests/test_offsets.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pandas.tseries.frequencies import _offset_map
1414
from pandas.tseries.index import _to_m8
1515
from pandas.tseries.tools import parse_time_string
16+
import pandas.tseries.offsets as offsets
1617

1718
from nose.tools import assert_raises
1819

@@ -953,6 +954,11 @@ def test_onOffset(self):
953954
assertOnOffset(offset, date, expected)
954955

955956
class TestBYearBegin(unittest.TestCase):
957+
958+
def test_misspecified(self):
959+
self.assertRaises(ValueError, BYearBegin, month=13)
960+
self.assertRaises(ValueError, BYearEnd, month=13)
961+
956962
def test_offset(self):
957963
tests = []
958964

@@ -996,6 +1002,9 @@ def test_offset(self):
9961002

9971003
class TestYearBegin(unittest.TestCase):
9981004

1005+
def test_misspecified(self):
1006+
self.assertRaises(ValueError, YearBegin, month=13)
1007+
9991008
def test_offset(self):
10001009
tests = []
10011010

@@ -1133,6 +1142,9 @@ def test_onOffset(self):
11331142

11341143
class TestYearEnd(unittest.TestCase):
11351144

1145+
def test_misspecified(self):
1146+
self.assertRaises(ValueError, YearEnd, month=13)
1147+
11361148
def test_offset(self):
11371149
tests = []
11381150

@@ -1188,7 +1200,8 @@ def test_offset(self):
11881200
datetime(2008, 2, 15): datetime(2008, 3, 31),
11891201
datetime(2008, 3, 31): datetime(2009, 3, 31),
11901202
datetime(2008, 3, 30): datetime(2008, 3, 31),
1191-
datetime(2005, 3, 31): datetime(2006, 3, 31),}))
1203+
datetime(2005, 3, 31): datetime(2006, 3, 31),
1204+
datetime(2006, 7, 30): datetime(2007, 3, 31)}))
11921205

11931206
tests.append((YearEnd(0, month=3),
11941207
{datetime(2008, 1, 1): datetime(2008, 3, 31),
@@ -1358,8 +1371,23 @@ def test_rule_code(self):
13581371
assert alias == _offset_map[alias].rule_code
13591372
assert alias == (_offset_map[alias] * 5).rule_code
13601373

1374+
def test_apply_ticks():
1375+
result = offsets.Hour(3).apply(offsets.Hour(4))
1376+
exp = offsets.Hour(7)
1377+
assert(result == exp)
1378+
1379+
def test_delta_to_tick():
1380+
delta = timedelta(3)
1381+
1382+
tick = offsets._delta_to_tick(delta)
1383+
assert(tick == offsets.Day(3))
13611384

1385+
def test_dateoffset_misc():
1386+
oset = offsets.DateOffset(months=2, days=4)
1387+
# it works
1388+
result = oset.freqstr
13621389

1390+
assert(not offsets.DateOffset(months=2) == 2)
13631391

13641392
if __name__ == '__main__':
13651393
import nose

pandas/tseries/tools.py

-8
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
print 'Please install python-dateutil via easy_install or some method!'
2222
raise # otherwise a 2nd import won't show the message
2323

24-
def _delta_to_microseconds(delta):
25-
return (delta.days * 24 * 60 * 60 * 1000000
26-
+ delta.seconds * 1000000
27-
+ delta.microseconds)
2824

2925
def _infer_tzinfo(start, end):
3026
def _infer(a, b):
@@ -214,8 +210,6 @@ def parse_time_string(arg, freq=None):
214210
repl[attr] = value
215211
if not stopped:
216212
reso = attr
217-
else:
218-
raise DateParseError("Missing attribute before %s" % attr)
219213
else:
220214
stopped = True
221215
break
@@ -243,8 +237,6 @@ def _try_parse_monthly(arg):
243237
return ret
244238

245239
def normalize_date(dt):
246-
if isinstance(dt, np.datetime64):
247-
dt = lib.Timestamp(dt)
248240
return dt.replace(hour=0, minute=0, second=0, microsecond=0)
249241

250242

test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
coverage erase
33
# nosetests pandas/tests/test_index.py --with-coverage --cover-package=pandas.core --pdb-failure --pdb
44
#nosetests -w pandas --with-coverage --cover-package=pandas --pdb-failure --pdb #--cover-inclusive
5-
nosetests -A "not slow" -w pandas/tseries --with-coverage --cover-package=pandas.tseries $* #--cover-inclusive
6-
#nosetests -w pandas --with-coverage --cover-package=pandas $*
5+
#nosetests -A "not slow" -w pandas/tseries --with-coverage --cover-package=pandas.tseries $* #--cover-inclusive
6+
nosetests -w pandas --with-coverage --cover-package=pandas $*
77
# nosetests -w pandas/io --with-coverage --cover-package=pandas.io --pdb-failure --pdb
88
# nosetests -w pandas/core --with-coverage --cover-package=pandas.core --pdb-failure --pdb
99
# nosetests -w pandas/stats --with-coverage --cover-package=pandas.stats

0 commit comments

Comments
 (0)