Skip to content

Commit 3e2a7f4

Browse files
committed
BUG: more bug fixes, have to fix intraday frequencies still
1 parent 24f1f3a commit 3e2a7f4

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

pandas/src/period.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -1024,23 +1024,23 @@ npy_int64 get_period_ordinal(int year, int month, int day,
10241024

10251025
if (freq == FR_SEC) {
10261026
absdays = absdate_from_ymd(year, month, day);
1027-
delta = (absdays - ORD_OFFSET - HIGHFREQ_ORIG);
1028-
return (npy_int64)(delta*86400 + hour*3600 + minute*60 + second + 1);
1027+
delta = (absdays - ORD_OFFSET + HIGHFREQ_ORIG);
1028+
return (npy_int64)(delta*86400 + hour*3600 + minute*60 + second);
10291029
}
10301030

10311031
if (freq == FR_MIN) {
10321032
absdays = absdate_from_ymd(year, month, day);
1033-
delta = (absdays - ORD_OFFSET - HIGHFREQ_ORIG);
1034-
return (npy_int64)(delta*1440 + hour*60 + minute + 1);
1033+
delta = (absdays - ORD_OFFSET + HIGHFREQ_ORIG);
1034+
return (npy_int64)(delta*1440 + hour*60 + minute);
10351035
}
10361036

10371037
if (freq == FR_HR) {
10381038
if ((absdays = absdate_from_ymd(year, month, day)) == INT_ERR_CODE)
10391039
{
10401040
goto onError;
10411041
}
1042-
delta = (absdays - ORD_OFFSET - HIGHFREQ_ORIG);
1043-
return (npy_int64)(delta*24 + hour + 1);
1042+
delta = (absdays - ORD_OFFSET + HIGHFREQ_ORIG);
1043+
return (npy_int64)(delta*24 + hour);
10441044
}
10451045

10461046
if (freq == FR_DAY)
@@ -1347,7 +1347,7 @@ static int _quarter_year(npy_int64 ordinal, int freq, int *year, int *quarter) {
13471347
asfreq_info af_info;
13481348
int qtr_freq;
13491349

1350-
ordinal = get_python_ordinal(ordinal, freq);
1350+
ordinal = get_python_ordinal(ordinal, freq) - ORD_OFFSET;
13511351

13521352
if (get_freq_group(freq) == FR_QTR)
13531353
qtr_freq = freq;

pandas/tseries/period.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import pandas.tseries.frequencies as _freq_mod
1111

1212
import pandas.core.common as com
13-
from pandas.util import py3compat
1413

1514
from pandas._tseries import Timestamp
1615
import pandas._tseries as lib
@@ -470,6 +469,30 @@ def dt64arr_to_periodarr(data, freq):
470469

471470
# --- Period index sketch
472471

472+
473+
def _period_index_cmp(opname):
474+
"""
475+
Wrap comparison operations to convert datetime-like to datetime64
476+
"""
477+
def wrapper(self, other):
478+
if isinstance(other, Period):
479+
func = getattr(self.values, opname)
480+
assert(other.freq == self.freq)
481+
result = func(other.ordinal)
482+
elif isinstance(other, PeriodIndex):
483+
assert(other.freq == self.freq)
484+
return getattr(self.values, opname)(other.values)
485+
else:
486+
other = Period(other, freq=self.freq)
487+
func = getattr(self.values, opname)
488+
result = func(other.ordinal)
489+
try:
490+
return result.view(np.ndarray)
491+
except:
492+
return result
493+
return wrapper
494+
495+
473496
class PeriodIndex(Int64Index):
474497
"""
475498
Immutable ndarray holding ordinal values indicating regular periods in
@@ -507,6 +530,13 @@ class PeriodIndex(Int64Index):
507530
"""
508531
_box_scalars = True
509532

533+
__eq__ = _period_index_cmp('__eq__')
534+
__ne__ = _period_index_cmp('__ne__')
535+
__lt__ = _period_index_cmp('__lt__')
536+
__gt__ = _period_index_cmp('__gt__')
537+
__le__ = _period_index_cmp('__le__')
538+
__ge__ = _period_index_cmp('__ge__')
539+
510540
def __new__(cls, data=None,
511541
freq=None, start=None, end=None, periods=None,
512542
copy=False, name=None):

pandas/tseries/tests/test_period.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_period_constructor(self):
145145
self.assertEqual(i1, expected)
146146

147147
i1 = Period(ordinal=200701, freq='M')
148-
self.assertEqual(i1.year, 16726)
148+
self.assertEqual(i1.year, 18695)
149149

150150
self.assertRaises(ValueError, Period, ordinal=200701)
151151

@@ -1494,13 +1494,13 @@ def test_daily(self):
14941494
self._check_freq('D', '1970-01-01')
14951495

14961496
def test_hourly(self):
1497-
self._check_freq('D', '1970-01-01')
1497+
self._check_freq('H', '1970-01-01')
14981498

14991499
def test_minutely(self):
1500-
self._check_freq('H', '1970-01-01 00:00:00')
1500+
self._check_freq('T', '1970-01-01')
15011501

15021502
def test_secondly(self):
1503-
self._check_freq('T', '1970-01-01 00:00:00')
1503+
self._check_freq('S', '1970-01-01')
15041504

15051505
def _check_freq(self, freq, base_date):
15061506
rng = PeriodIndex(start=base_date, periods=10, freq=freq)

0 commit comments

Comments
 (0)