Skip to content

Commit bfa8066

Browse files
committed
move period related functions from tslib to period module
Moved many import statement to local scope in period module, similar to tslib. This fixes circular dependency problems.
1 parent a05115b commit bfa8066

File tree

9 files changed

+649
-603
lines changed

9 files changed

+649
-603
lines changed

pandas/period.pyx

+612-13
Large diffs are not rendered by default.

pandas/tests/test_tseries.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pandas.util.testing as tm
88
from pandas.compat import range, lrange, zip
99
import pandas.lib as lib
10+
import pandas.period as period
1011
import pandas.algos as algos
1112

1213

@@ -731,12 +732,10 @@ def test_to_datetime_bijective(self):
731732
class TestPeriodField(tm.TestCase):
732733

733734
def test_get_period_field_raises_on_out_of_range(self):
734-
from pandas import tslib
735-
self.assertRaises(ValueError, tslib.get_period_field, -1, 0, 0)
735+
self.assertRaises(ValueError, period.get_period_field, -1, 0, 0)
736736

737737
def test_get_period_field_array_raises_on_out_of_range(self):
738-
from pandas import tslib
739-
self.assertRaises(ValueError, tslib.get_period_field_arr, -1, np.empty(1), 0)
738+
self.assertRaises(ValueError, period.get_period_field_arr, -1, np.empty(1), 0)
740739

741740
if __name__ == '__main__':
742741
import nose

pandas/tseries/frequencies.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import pandas.core.common as com
1313
import pandas.lib as lib
1414
import pandas.tslib as tslib
15+
import pandas.period as period
1516
from pandas.tslib import Timedelta
1617

1718
class FreqGroup(object):
@@ -31,12 +32,12 @@ class FreqGroup(object):
3132

3233
class Resolution(object):
3334

34-
RESO_US = tslib.US_RESO
35-
RESO_MS = tslib.MS_RESO
36-
RESO_SEC = tslib.S_RESO
37-
RESO_MIN = tslib.T_RESO
38-
RESO_HR = tslib.H_RESO
39-
RESO_DAY = tslib.D_RESO
35+
RESO_US = period.US_RESO
36+
RESO_MS = period.MS_RESO
37+
RESO_SEC = period.S_RESO
38+
RESO_MIN = period.T_RESO
39+
RESO_HR = period.H_RESO
40+
RESO_DAY = period.D_RESO
4041

4142
_reso_str_map = {
4243
RESO_US: 'microsecond',

pandas/tseries/index.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from pandas.lib import Timestamp
2929
import pandas.lib as lib
3030
import pandas.tslib as tslib
31+
import pandas.period as period
3132
import pandas.algos as _algos
3233
import pandas.index as _index
3334

@@ -1461,7 +1462,7 @@ def is_normalized(self):
14611462

14621463
@cache_readonly
14631464
def _resolution(self):
1464-
return tslib.resolution(self.asi8, self.tz)
1465+
return period.resolution(self.asi8, self.tz)
14651466

14661467
def equals(self, other):
14671468
"""

pandas/tseries/period.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import pandas.tseries.offsets as offsets
1414

1515
from pandas.period import Period
16+
import pandas.period as period
1617
from pandas.period import (
18+
get_period_field_arr,
1719
_validate_end_alias,
1820
_quarter_to_myear,
1921
)
@@ -32,7 +34,7 @@
3234
def _field_accessor(name, alias, docstring=None):
3335
def f(self):
3436
base, mult = _gfc(self.freq)
35-
return tslib.get_period_field_arr(alias, self.values, base)
37+
return get_period_field_arr(alias, self.values, base)
3638
f.__name__ = name
3739
f.__doc__ = docstring
3840
return property(f)
@@ -41,7 +43,7 @@ def f(self):
4143
def _get_ordinals(data, freq):
4244
f = lambda x: Period(x, freq=freq).ordinal
4345
if isinstance(data[0], Period):
44-
return tslib.extract_ordinals(data, freq)
46+
return period.extract_ordinals(data, freq)
4547
else:
4648
return lib.map_infer(data, f)
4749

@@ -51,7 +53,7 @@ def dt64arr_to_periodarr(data, freq, tz):
5153
raise ValueError('Wrong dtype: %s' % data.dtype)
5254

5355
base, mult = _gfc(freq)
54-
return tslib.dt64arr_to_periodarr(data.view('i8'), base, tz)
56+
return period.dt64arr_to_periodarr(data.view('i8'), base, tz)
5557

5658
# --- Period index sketch
5759

@@ -236,7 +238,7 @@ def _from_arraylike(cls, data, freq, tz):
236238
else:
237239
base1, _ = _gfc(data.freq)
238240
base2, _ = _gfc(freq)
239-
data = tslib.period_asfreq_arr(data.values, base1,
241+
data = period.period_asfreq_arr(data.values, base1,
240242
base2, 1)
241243
else:
242244
if freq is None and len(data) > 0:
@@ -363,7 +365,7 @@ def asfreq(self, freq=None, how='E'):
363365
raise ValueError('Only mult == 1 supported')
364366

365367
end = how == 'E'
366-
new_data = tslib.period_asfreq_arr(self.values, base1, base2, end)
368+
new_data = period.period_asfreq_arr(self.values, base1, base2, end)
367369
return self._simple_new(new_data, self.name, freq=freq)
368370

369371
def to_datetime(self, dayfirst=False):
@@ -431,7 +433,7 @@ def to_timestamp(self, freq=None, how='start'):
431433
base, mult = _gfc(freq)
432434
new_data = self.asfreq(freq, how)
433435

434-
new_data = tslib.periodarr_to_dt64arr(new_data.values, base)
436+
new_data = period.periodarr_to_dt64arr(new_data.values, base)
435437
return DatetimeIndex(new_data, freq='infer', name=self.name)
436438

437439
def _add_delta(self, other):
@@ -881,7 +883,7 @@ def _range_from_fields(year=None, month=None, quarter=None, day=None,
881883
year, quarter = _make_field_arrays(year, quarter)
882884
for y, q in zip(year, quarter):
883885
y, m = _quarter_to_myear(y, q, freq)
884-
val = tslib.period_ordinal(y, m, 1, 1, 1, 1, 0, 0, base)
886+
val = period.period_ordinal(y, m, 1, 1, 1, 1, 0, 0, base)
885887
ordinals.append(val)
886888
else:
887889
base, mult = _gfc(freq)
@@ -890,7 +892,7 @@ def _range_from_fields(year=None, month=None, quarter=None, day=None,
890892

891893
arrays = _make_field_arrays(year, month, day, hour, minute, second)
892894
for y, mth, d, h, mn, s in zip(*arrays):
893-
ordinals.append(tslib.period_ordinal(y, mth, d, h, mn, s, 0, 0, base))
895+
ordinals.append(period.period_ordinal(y, mth, d, h, mn, s, 0, 0, base))
894896

895897
return np.array(ordinals, dtype=np.int64), freq
896898

pandas/tseries/tests/test_tslib.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import numpy as np
44

55
from pandas import tslib
6+
from pandas import period
67
import datetime
78

89
from pandas.core.api import Timestamp, Series, Timedelta
9-
from pandas.tslib import period_asfreq, period_ordinal, get_timezone
10+
from pandas.tslib import get_timezone
11+
from pandas.period import period_asfreq, period_ordinal
1012
from pandas.tseries.index import date_range
1113
from pandas.tseries.frequencies import get_freq
1214
import pandas.tseries.offsets as offsets
@@ -764,11 +766,11 @@ def test_addition_subtraction_preserve_frequency(self):
764766
def test_resolution(self):
765767

766768
for freq, expected in zip(['A', 'Q', 'M', 'D', 'H', 'T', 'S', 'L', 'U'],
767-
[tslib.D_RESO, tslib.D_RESO, tslib.D_RESO, tslib.D_RESO,
768-
tslib.H_RESO, tslib.T_RESO,tslib.S_RESO, tslib.MS_RESO, tslib.US_RESO]):
769+
[period.D_RESO, period.D_RESO, period.D_RESO, period.D_RESO,
770+
period.H_RESO, period.T_RESO, period.S_RESO, period.MS_RESO, period.US_RESO]):
769771
for tz in [None, 'Asia/Tokyo', 'US/Eastern', 'dateutil/US/Eastern']:
770772
idx = date_range(start='2013-04-01', periods=30, freq=freq, tz=tz)
771-
result = tslib.resolution(idx.asi8, idx.tz)
773+
result = period.resolution(idx.asi8, idx.tz)
772774
self.assertEqual(result, expected)
773775

774776

pandas/tslib.pxd

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ from numpy cimport ndarray, int64_t
22

33
cdef convert_to_tsobject(object, object, object)
44
cdef convert_to_timedelta64(object, object, object)
5+
cpdef object maybe_get_tz(object)
6+
cdef bint _is_utc(object)
7+
cdef bint _is_tzlocal(object)
8+
cdef object _get_dst_info(object)

0 commit comments

Comments
 (0)