Skip to content

Commit

Permalink
BUG: fix NumPy 1.7 argmin workaround, test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Jun 20, 2012
1 parent de8f14d commit ec221c6
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 38 deletions.
2 changes: 1 addition & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ def insert(self, loc, item, value):
# new block
self._add_new_block(item, value, loc=loc)

if len(self.blocks) > 20:
if len(self.blocks) > 100:
self._consolidate_inplace()

def set_items_norename(self, value):
Expand Down
12 changes: 5 additions & 7 deletions pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,11 @@ def infer_freq(index, warn=True):
freq : string or None
None if no discernable frequency
"""
from pandas.tseries.index import DatetimeIndex

if not isinstance(index, DatetimeIndex):
index = DatetimeIndex(index)

inferer = _FrequencyInferer(index, warn=warn)
return inferer.get_freq()

Expand All @@ -710,11 +715,6 @@ class _FrequencyInferer(object):
"""

def __init__(self, index, warn=True):
from pandas.tseries.index import DatetimeIndex

if not isinstance(index, DatetimeIndex):
index = DatetimeIndex(index)

self.index = index
self.values = np.asarray(index).view('i8')
self.warn = warn
Expand Down Expand Up @@ -967,8 +967,6 @@ def is_superperiod(source, target):
return target in ['D', 'B', 'H', 'T', 'S']

def _get_rule_month(source, default='DEC'):
if isinstance(source, offsets.DateOffset):
source = source.rule_code
source = source.upper()
if '-' not in source:
return default
Expand Down
7 changes: 5 additions & 2 deletions pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,12 @@ def searchsorted(self, key, side='left'):
def is_type_compatible(self, typ):
return typ == self.inferred_type or typ == 'datetime'

# hack to workaround argmin failure
def argmin(self):
return (-self).argmax()
# hack to workaround argmin failure
try:
return self.values.argmin()
except Exception: # pragma: no cover
return self.asi8.argmin()

@property
def inferred_type(self):
Expand Down
22 changes: 5 additions & 17 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ def rollback(self, someDate):

def rollforward(self, dt):
"""Roll provided date forward to next offset only if not on offset"""
if isinstance(dt, np.datetime64):
dt = Timestamp(dt)
if not self.onOffset(dt):
dt = dt + self.__class__(1, **self.kwds)
return dt
Expand Down Expand Up @@ -297,8 +295,6 @@ def apply(self, other):
'datetime or timedelta!')
@classmethod
def onOffset(cls, dt):
if isinstance(dt, np.datetime64):
dt = Timestamp(dt)
return dt.weekday() < 5


Expand Down Expand Up @@ -636,8 +632,6 @@ def apply(self, other):
first = _get_firstbday(wkday)

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

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

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

if monthsSince == 3: # on an offset
monthsSince = 0

if n <= 0 and monthsSince != 0:
# make sure you roll forward, so negate
monthsSince = monthsSince - 3
Expand All @@ -756,7 +747,7 @@ def __init__(self, n=1, **kwds):
self.month = kwds.get('month', 12)

if self.month < 1 or self.month > 12:
raise Exception('Month must go from 1 to 12')
raise ValueError('Month must go from 1 to 12')

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

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

if self.month < 1 or self.month > 12:
raise Exception('Month must go from 1 to 12')
raise ValueError('Month must go from 1 to 12')

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

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

if self.month < 1 or self.month > 12:
raise Exception('Month must go from 1 to 12')
raise ValueError('Month must go from 1 to 12')

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

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

if self.month < 1 or self.month > 12:
raise Exception('Month must go from 1 to 12')
raise ValueError('Month must go from 1 to 12')

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

Expand Down Expand Up @@ -1012,7 +1003,7 @@ def _delta_to_tick(delta):
return Milli(nanos // 1000000)
elif nanos % 1000 == 0:
return Micro(nanos // 1000)
else:
else: # pragma: no cover
return Nano(nanos)

def _delta_to_nanoseconds(delta):
Expand Down Expand Up @@ -1114,9 +1105,6 @@ def generate_range(start=None, end=None, periods=None,
end = None
periods = 0

if _count_not_none(start, end, periods) < 2:
raise ValueError('Must specify 2 of start, end, periods')

if end is None:
end = start + (periods - 1) * offset

Expand Down
13 changes: 13 additions & 0 deletions pandas/tseries/tests/test_frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from pandas.tseries.frequencies import to_offset, infer_freq
from pandas.tseries.tools import to_datetime
import pandas.tseries.frequencies as fmod
import pandas.tseries.offsets as offsets

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

def test_non_datetimeindex(self):
rng = _dti(['1/31/2000', '1/31/2001', '1/31/2002'])

vals = rng.to_pydatetime()

result = infer_freq(vals)
self.assertEqual(result, rng.inferred_freq)

MONTHS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP',
'OCT', 'NOV', 'DEC']

def test_is_superperiod_subperiod():
assert(fmod.is_superperiod(offsets.YearEnd(), offsets.MonthEnd()))
assert(fmod.is_subperiod(offsets.MonthEnd(), offsets.YearEnd()))

if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
Expand Down
30 changes: 29 additions & 1 deletion pandas/tseries/tests/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pandas.tseries.frequencies import _offset_map
from pandas.tseries.index import _to_m8
from pandas.tseries.tools import parse_time_string
import pandas.tseries.offsets as offsets

from nose.tools import assert_raises

Expand Down Expand Up @@ -953,6 +954,11 @@ def test_onOffset(self):
assertOnOffset(offset, date, expected)

class TestBYearBegin(unittest.TestCase):

def test_misspecified(self):
self.assertRaises(ValueError, BYearBegin, month=13)
self.assertRaises(ValueError, BYearEnd, month=13)

def test_offset(self):
tests = []

Expand Down Expand Up @@ -996,6 +1002,9 @@ def test_offset(self):

class TestYearBegin(unittest.TestCase):

def test_misspecified(self):
self.assertRaises(ValueError, YearBegin, month=13)

def test_offset(self):
tests = []

Expand Down Expand Up @@ -1133,6 +1142,9 @@ def test_onOffset(self):

class TestYearEnd(unittest.TestCase):

def test_misspecified(self):
self.assertRaises(ValueError, YearEnd, month=13)

def test_offset(self):
tests = []

Expand Down Expand Up @@ -1188,7 +1200,8 @@ def test_offset(self):
datetime(2008, 2, 15): datetime(2008, 3, 31),
datetime(2008, 3, 31): datetime(2009, 3, 31),
datetime(2008, 3, 30): datetime(2008, 3, 31),
datetime(2005, 3, 31): datetime(2006, 3, 31),}))
datetime(2005, 3, 31): datetime(2006, 3, 31),
datetime(2006, 7, 30): datetime(2007, 3, 31)}))

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

def test_apply_ticks():
result = offsets.Hour(3).apply(offsets.Hour(4))
exp = offsets.Hour(7)
assert(result == exp)

def test_delta_to_tick():
delta = timedelta(3)

tick = offsets._delta_to_tick(delta)
assert(tick == offsets.Day(3))

def test_dateoffset_misc():
oset = offsets.DateOffset(months=2, days=4)
# it works
result = oset.freqstr

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

if __name__ == '__main__':
import nose
Expand Down
8 changes: 0 additions & 8 deletions pandas/tseries/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
print 'Please install python-dateutil via easy_install or some method!'
raise # otherwise a 2nd import won't show the message

def _delta_to_microseconds(delta):
return (delta.days * 24 * 60 * 60 * 1000000
+ delta.seconds * 1000000
+ delta.microseconds)

def _infer_tzinfo(start, end):
def _infer(a, b):
Expand Down Expand Up @@ -214,8 +210,6 @@ def parse_time_string(arg, freq=None):
repl[attr] = value
if not stopped:
reso = attr
else:
raise DateParseError("Missing attribute before %s" % attr)
else:
stopped = True
break
Expand Down Expand Up @@ -243,8 +237,6 @@ def _try_parse_monthly(arg):
return ret

def normalize_date(dt):
if isinstance(dt, np.datetime64):
dt = lib.Timestamp(dt)
return dt.replace(hour=0, minute=0, second=0, microsecond=0)


Expand Down
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
coverage erase
# nosetests pandas/tests/test_index.py --with-coverage --cover-package=pandas.core --pdb-failure --pdb
#nosetests -w pandas --with-coverage --cover-package=pandas --pdb-failure --pdb #--cover-inclusive
nosetests -A "not slow" -w pandas/tseries --with-coverage --cover-package=pandas.tseries $* #--cover-inclusive
#nosetests -w pandas --with-coverage --cover-package=pandas $*
#nosetests -A "not slow" -w pandas/tseries --with-coverage --cover-package=pandas.tseries $* #--cover-inclusive
nosetests -w pandas --with-coverage --cover-package=pandas $*
# nosetests -w pandas/io --with-coverage --cover-package=pandas.io --pdb-failure --pdb
# nosetests -w pandas/core --with-coverage --cover-package=pandas.core --pdb-failure --pdb
# nosetests -w pandas/stats --with-coverage --cover-package=pandas.stats
Expand Down

0 comments on commit ec221c6

Please sign in to comment.