本文整理匯總了Python中pandas.DateOffset方法的典型用法代碼示例。如果您正苦於以下問題:Python pandas.DateOffset方法的具體用法?Python pandas.DateOffset怎麽用?Python pandas.DateOffset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas
的用法示例。
在下文中一共展示了pandas.DateOffset方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: setup
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def setup(self):
"""Runs before every method to create a clean testing setup"""
# Load a test instrument
self.testInst = pysat.Instrument()
self.testInst.data = pds.DataFrame({'Kp': np.arange(0, 4, 1.0/3.0),
'ap_nan': np.full(shape=12, \
fill_value=np.nan),
'ap_inf': np.full(shape=12, \
fill_value=np.inf)},
index=[pysat.datetime(2009, 1, 1)
+ pds.DateOffset(hours=3*i)
for i in range(12)])
self.testInst.meta = pysat.Meta()
self.testInst.meta.__setitem__('Kp', {self.testInst.meta.fill_label:
np.nan})
self.testInst.meta.__setitem__('ap_nan', {self.testInst.meta.fill_label:
np.nan})
self.testInst.meta.__setitem__('ap_inv', {self.testInst.meta.fill_label:
np.inf})
# Load a test Metadata
self.testMeta = pysat.Meta()
示例2: test_calc_f107a_high_rate
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def test_calc_f107a_high_rate(self):
""" Test the calc_f107a routine with sub-daily data"""
self.testInst.data = pds.DataFrame({'f107': np.linspace(70, 200,
3840)},
index=[pysat.datetime(2009, 1, 1)
+ pds.DateOffset(hours=i)
for i in range(3840)])
sw_f107.calc_f107a(self.testInst, f107_name='f107', f107a_name='f107a')
# Assert that new data and metadata exist
assert 'f107a' in self.testInst.data.columns
assert 'f107a' in self.testInst.meta.keys()
# Assert the values are finite and realistic means
assert np.all(np.isfinite(self.testInst['f107a']))
assert self.testInst['f107a'].min() > self.testInst['f107'].min()
assert self.testInst['f107a'].max() < self.testInst['f107'].max()
# Assert the same mean value is used for a day
assert len(np.unique(self.testInst['f107a'][:24])) == 1
示例3: test_calc_f107a_daily_missing
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def test_calc_f107a_daily_missing(self):
""" Test the calc_f107a routine with some daily data missing"""
self.testInst.data = pds.DataFrame({'f107': np.linspace(70, 200, 160)},
index=[pysat.datetime(2009, 1, 1)
+ pds.DateOffset(days=2*i+1)
for i in range(160)])
sw_f107.calc_f107a(self.testInst, f107_name='f107', f107a_name='f107a')
# Assert that new data and metadata exist
assert 'f107a' in self.testInst.data.columns
assert 'f107a' in self.testInst.meta.keys()
# Assert the finite values have realistic means
assert(np.nanmin(self.testInst['f107a'])
> np.nanmin(self.testInst['f107']))
assert(np.nanmax(self.testInst['f107a'])
< np.nanmax(self.testInst['f107']))
# Assert the expected number of fill values
assert(len(self.testInst['f107a'][np.isnan(self.testInst['f107a'])])
== 40)
示例4: setup
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def setup(self):
"""Runs before every method to create a clean testing setup."""
info = {'index': 'mlt'}
self.testInst = pysat.Instrument('pysat', 'testing',
clean_level='clean',
orbit_info=info)
times = [[pysat.datetime(2008, 12, 31, 4),
pysat.datetime(2008, 12, 31, 5, 37)],
[pysat.datetime(2009, 1, 1),
pysat.datetime(2009, 1, 1, 1, 37)]
]
for seconds in np.arange(38):
day = pysat.datetime(2009, 1, 2) + \
pds.DateOffset(days=int(seconds))
times.append([day, day +
pds.DateOffset(hours=1, minutes=37,
seconds=int(seconds)) -
pds.DateOffset(seconds=20)])
self.testInst.custom.add(filter_data2, 'modify', times=times)
示例5: test_single_adding_custom_function_wrong_times
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def test_single_adding_custom_function_wrong_times(self):
"""Only the data at the correct time should be accepted, otherwise it
returns nan
"""
def custom1(inst):
new_index = inst.index+pds.DateOffset(milliseconds=500)
d = pds.Series(2.0 * inst['mlt'], index=new_index)
d.name = 'doubleMLT'
print(new_index)
return d
self.add(custom1, 'add')
self.testInst.load(2009, 1)
ans = (self.testInst['doubleMLT'].isnull()).all()
if self.testInst.pandas_format:
assert ans
else:
print("Warning! Xarray doesn't enforce the same times on all " +
"parameters in dataset.")
示例6: _apply_loffset
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def _apply_loffset(self, result):
"""
If loffset is set, offset the result index.
This is NOT an idempotent routine, it will be applied
exactly once to the result.
Parameters
----------
result : Series or DataFrame
the result of resample
"""
needs_offset = (
isinstance(self.loffset, (DateOffset, timedelta,
np.timedelta64)) and
isinstance(result.index, DatetimeIndex) and
len(result.index) > 0
)
if needs_offset:
result.index = result.index + self.loffset
self.loffset = None
return result
示例7: test_dt64_with_offset_array
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def test_dt64_with_offset_array(klass, assert_func):
# GH#10699
# array of offsets
box = Series if klass is Series else pd.Index
with tm.assert_produces_warning(PerformanceWarning):
s = klass([Timestamp('2000-1-1'), Timestamp('2000-2-1')])
result = s + box([pd.offsets.DateOffset(years=1),
pd.offsets.MonthEnd()])
exp = klass([Timestamp('2001-1-1'), Timestamp('2000-2-29')])
assert_func(result, exp)
# same offset
result = s + box([pd.offsets.DateOffset(years=1),
pd.offsets.DateOffset(years=1)])
exp = klass([Timestamp('2001-1-1'), Timestamp('2001-2-1')])
assert_func(result, exp)
示例8: test_select_has_data
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def test_select_has_data():
algo = algos.SelectHasData(min_count=3, lookback=pd.DateOffset(days=3))
s = bt.Strategy('s')
dts = pd.date_range('2010-01-01', periods=10)
data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100.)
data['c1'].ix[dts[0]] = np.nan
data['c1'].ix[dts[1]] = np.nan
s.setup(data)
s.update(dts[2])
assert algo(s)
selected = s.temp['selected']
assert len(selected) == 1
assert 'c2' in selected
示例9: test_select_has_data_preselected
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def test_select_has_data_preselected():
algo = algos.SelectHasData(min_count=3, lookback=pd.DateOffset(days=3))
s = bt.Strategy('s')
dts = pd.date_range('2010-01-01', periods=3)
data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100.)
data['c1'].ix[dts[0]] = np.nan
data['c1'].ix[dts[1]] = np.nan
s.setup(data)
s.update(dts[2])
s.temp['selected'] = ['c1']
assert algo(s)
selected = s.temp['selected']
assert len(selected) == 0
示例10: test_weigh_erc
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def test_weigh_erc(mock_erc):
algo = algos.WeighERC(lookback=pd.DateOffset(days=5))
mock_erc.return_value = pd.Series({'c1': 0.3, 'c2': 0.7})
s = bt.Strategy('s')
dts = pd.date_range('2010-01-01', periods=5)
data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100.)
s.setup(data)
s.update(dts[4])
s.temp['selected'] = ['c1', 'c2']
assert algo(s)
assert mock_erc.called
rets = mock_erc.call_args[0][0]
assert len(rets) == 4
assert 'c1' in rets
assert 'c2' in rets
weights = s.temp['weights']
assert len(weights) == 2
assert weights['c1'] == 0.3
assert weights['c2'] == 0.7
示例11: test_weigh_mean_var
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def test_weigh_mean_var(mock_mv):
algo = algos.WeighMeanVar(lookback=pd.DateOffset(days=5))
mock_mv.return_value = pd.Series({'c1': 0.3, 'c2': 0.7})
s = bt.Strategy('s')
dts = pd.date_range('2010-01-01', periods=5)
data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100.)
s.setup(data)
s.update(dts[4])
s.temp['selected'] = ['c1', 'c2']
assert algo(s)
assert mock_mv.called
rets = mock_mv.call_args[0][0]
assert len(rets) == 4
assert 'c1' in rets
assert 'c2' in rets
weights = s.temp['weights']
assert len(weights) == 2
assert weights['c1'] == 0.3
assert weights['c2'] == 0.7
示例12: test_select_momentum
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def test_select_momentum():
algo = algos.SelectMomentum(n=1, lookback=pd.DateOffset(days=3))
s = bt.Strategy('s')
dts = pd.date_range('2010-01-01', periods=3)
data = pd.DataFrame(index=dts, columns=['c1', 'c2'], data=100.)
data['c1'].ix[dts[2]] = 105
data['c2'].ix[dts[2]] = 95
s.setup(data)
s.update(dts[2])
s.temp['selected'] = ['c1', 'c2']
assert algo(s)
actual = s.temp['selected']
assert len(actual) == 1
assert 'c1' in actual
示例13: apply_time_offset
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def apply_time_offset(time, years=0, months=0, days=0, hours=0):
"""Apply a specified offset to the given time array.
This is useful for GFDL model output of instantaneous values. For example,
3 hourly data postprocessed to netCDF files spanning 1 year each will
actually have time values that are offset by 3 hours, such that the first
value is for 1 Jan 03:00 and the last value is 1 Jan 00:00 of the
subsequent year. This causes problems in xarray, e.g. when trying to group
by month. It is resolved by manually subtracting off those three hours,
such that the dates span from 1 Jan 00:00 to 31 Dec 21:00 as desired.
Parameters
----------
time : xarray.DataArray representing a timeseries
years, months, days, hours : int, optional
The number of years, months, days, and hours, respectively, to offset
the time array by. Positive values move the times later.
Returns
-------
pandas.DatetimeIndex
Examples
--------
Case of a length-1 input time array:
>>> times = xr.DataArray(datetime.datetime(1899, 12, 31, 21))
>>> apply_time_offset(times)
Timestamp('1900-01-01 00:00:00')
Case of input time array with length greater than one:
>>> times = xr.DataArray([datetime.datetime(1899, 12, 31, 21),
... datetime.datetime(1899, 1, 31, 21)])
>>> apply_time_offset(times) # doctest: +NORMALIZE_WHITESPACE
DatetimeIndex(['1900-01-01', '1899-02-01'], dtype='datetime64[ns]',
freq=None)
"""
return (pd.to_datetime(time.values) +
pd.DateOffset(years=years, months=months, days=days, hours=hours))
示例14: test_apply_time_offset
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def test_apply_time_offset():
start = datetime.datetime(1900, 5, 10)
years, months, days, hours = -2, 1, 7, 3
# test lengths 0, 1, and >1 of input time array
for periods in range(3):
times = pd.date_range(start=start, freq='M', periods=periods)
times = pd.to_datetime(times.values) # Workaround for pandas bug
actual = apply_time_offset(xr.DataArray(times), years=years,
months=months, days=days, hours=hours)
desired = (times + pd.DateOffset(
years=years, months=months, days=days, hours=hours
))
assert actual.identical(desired)
示例15: get_dataset
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import DateOffset [as 別名]
def get_dataset(src, name, distribution):
df = src[src.airport == name].copy()
del df['airport']
df['date'] = pd.to_datetime(df.date)
# timedelta here instead of pd.DateOffset to avoid pandas bug < 0.18 (Pandas issue #11925)
df['left'] = df.date - datetime.timedelta(days=0.5)
df['right'] = df.date + datetime.timedelta(days=0.5)
df = df.set_index(['date'])
df.sort_index(inplace=True)
if distribution == 'Smoothed':
window, order = 51, 3
for key in STATISTICS:
df[key] = savgol_filter(df[key], window, order)
return ColumnDataSource(data=df)