本文整理汇总了Python中pandas.to_datetime方法的典型用法代码示例。如果您正苦于以下问题:Python pandas.to_datetime方法的具体用法?Python pandas.to_datetime怎么用?Python pandas.to_datetime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas
的用法示例。
在下文中一共展示了pandas.to_datetime方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_monthly_mean_at_each_ind
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def test_monthly_mean_at_each_ind():
times_submonthly = pd.to_datetime(['2000-06-01', '2000-06-15',
'2000-07-04', '2000-07-19'])
times_means = pd.to_datetime(['2000-06-01', '2000-07-01'])
len_other_dim = 2
arr_submonthly = xr.DataArray(
np.random.random((len(times_submonthly), len_other_dim)),
dims=[TIME_STR, 'dim0'], coords={TIME_STR: times_submonthly}
)
arr_means = xr.DataArray(
np.random.random((len(times_means), len_other_dim)),
dims=arr_submonthly.dims, coords={TIME_STR: times_means}
)
actual = monthly_mean_at_each_ind(arr_means, arr_submonthly)
desired_values = np.stack([arr_means.values[0]] * len_other_dim +
[arr_means.values[1]] * len_other_dim,
axis=0)
desired = xr.DataArray(desired_values, dims=arr_submonthly.dims,
coords=arr_submonthly.coords)
assert actual.identical(desired)
示例2: test_yearly_average_no_mask
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def test_yearly_average_no_mask():
times = pd.to_datetime(['2000-06-01', '2000-06-15',
'2001-07-04', '2001-10-01', '2001-12-31',
'2004-01-01'])
arr = xr.DataArray(np.random.random((len(times),)),
dims=[TIME_STR], coords={TIME_STR: times})
dt = arr.copy(deep=True)
dt.values = np.random.random((len(times),))
actual = yearly_average(arr, dt)
yr2000 = (arr[0]*dt[0] + arr[1]*dt[1]) / (dt[0] + dt[1])
yr2001 = ((arr[2]*dt[2] + arr[3]*dt[3] + arr[4]*dt[4]) /
(dt[2] + dt[3] + dt[4]))
yr2004 = arr[-1]
yrs_coord = [2000, 2001, 2004]
yr_avgs = np.array([yr2000, yr2001, yr2004])
desired = xr.DataArray(yr_avgs, dims=['year'], coords={'year': yrs_coord})
xr.testing.assert_allclose(actual, desired)
示例3: simple_storage_model
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def simple_storage_model(request):
"""
Make a simple model with a single Input, Storage and Output.
Input -> Storage -> Output
"""
model = pywr.core.Model(
start=pandas.to_datetime('2016-01-01'),
end=pandas.to_datetime('2016-01-05'),
timestep=datetime.timedelta(1),
)
inpt = Input(model, name="Input", max_flow=5.0, cost=-1)
res = Storage(model, name="Storage", num_outputs=1, num_inputs=1, max_volume=20, initial_volume=10)
otpt = Output(model, name="Output", max_flow=8, cost=-999)
inpt.connect(res)
res.connect(otpt)
return model
示例4: test_storage_max_volume_zero
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def test_storage_max_volume_zero():
"""Test a that an max_volume of zero results in a NaN for current_pc and no exception
"""
model = Model(
start=pandas.to_datetime('2016-01-01'),
end=pandas.to_datetime('2016-01-01')
)
storage = Storage(model, 'storage', num_inputs=1, num_outputs=0)
otpt = Output(model, 'output', max_flow=99999, cost=-99999)
storage.connect(otpt)
storage.max_volume = 0
model.run()
assert np.isnan(storage.current_pc)
示例5: test_storage_max_volume_nested_param
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def test_storage_max_volume_nested_param():
"""Test that a max_volume can be used with a Parameter with children.
"""
model = Model(
start=pandas.to_datetime('2016-01-01'),
end=pandas.to_datetime('2016-01-01')
)
storage = Storage(model, 'storage', num_inputs=1, num_outputs=0)
otpt = Output(model, 'output', max_flow=99999, cost=-99999)
storage.connect(otpt)
p = AggregatedParameter(model, [ConstantParameter(model, 20.0), ConstantParameter(model, 20.0)], agg_func='sum')
storage.max_volume = p
storage.initial_volume = 10.0
model.setup()
np.testing.assert_allclose(storage.current_pc, 0.25)
示例6: test_storage_max_volume_param_raises
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def test_storage_max_volume_param_raises():
"""Test that a max_volume can not be used with 2 levels of child parameters.
"""
model = Model(
start=pandas.to_datetime('2016-01-01'),
end=pandas.to_datetime('2016-01-01')
)
storage = Storage(model, 'storage', num_inputs=1, num_outputs=0)
otpt = Output(model, 'output', max_flow=99999, cost=-99999)
storage.connect(otpt)
p = AggregatedParameter(model, [ConstantParameter(model, 20.0), ConstantParameter(model, 20.0)], agg_func='sum')
p1 = AggregatedParameter(model, [p, ConstantParameter(model, 1.5)], agg_func="product")
storage.max_volume = p1
storage.initial_volume = 10.0
with pytest.raises(RuntimeError):
model.run()
示例7: test_daily_profile_leap_day
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def test_daily_profile_leap_day(model):
"""Test behaviour of daily profile parameter for leap years
"""
inpt = Input(model, "input")
otpt = Output(model, "otpt", max_flow=None, cost=-999)
inpt.connect(otpt)
inpt.max_flow = DailyProfileParameter(model, np.arange(0, 366, dtype=np.float64))
# non-leap year
model.timestepper.start = pd.to_datetime("2015-01-01")
model.timestepper.end = pd.to_datetime("2015-12-31")
model.run()
assert_allclose(inpt.flow, 365) # NOT 364
# leap year
model.timestepper.start = pd.to_datetime("2016-01-01")
model.timestepper.end = pd.to_datetime("2016-12-31")
model.run()
assert_allclose(inpt.flow, 365)
示例8: test_scenario_daily_profile_leap_day
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def test_scenario_daily_profile_leap_day(self, simple_linear_model):
"""Test behaviour of daily profile parameter for leap years
"""
model = simple_linear_model
model.timestepper.start = pd.to_datetime("2016-01-01")
model.timestepper.end = pd.to_datetime("2016-12-31")
scenario = Scenario(model, 'A', 2)
values = np.array([np.arange(366, dtype=np.float64), np.arange(366, 0, -1, dtype=np.float64)])
expected_values = values.T
p = ScenarioDailyProfileParameter(model, scenario, values)
AssertionRecorder(model, p, expected_data=expected_values)
model.setup()
model.run()
示例9: test_simple_model_with_annual_licence_multi_year
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def test_simple_model_with_annual_licence_multi_year(simple_linear_model):
""" Test the AnnualLicense over multiple years
"""
import pandas as pd
import datetime, calendar
m = simple_linear_model
# Modify model to run for 3 years of non-leap years at 30 day time-step.
m.timestepper.start = pd.to_datetime('2017-1-1')
m.timestepper.end = pd.to_datetime('2020-1-1')
m.timestepper.delta = datetime.timedelta(30)
annual_total = 365.0
lic = AnnualLicense(m, m.nodes["Input"], annual_total)
# Apply licence to the model
m.nodes["Input"].max_flow = lic
m.nodes["Output"].max_flow = 10.0
m.nodes["Output"].cost = -10.0
m.setup()
for i in range(len(m.timestepper)):
m.step()
days_in_year = 365 + int(calendar.isleap(m.timestepper.current.datetime.year))
assert_allclose(m.nodes["Output"].flow, annual_total/days_in_year)
示例10: test_reset_timestepper_recorder
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def test_reset_timestepper_recorder():
model = Model(
start=pandas.to_datetime('2016-01-01'),
end=pandas.to_datetime('2016-01-01')
)
inpt = Input(model, "input", max_flow=10)
otpt = Output(model, "output", max_flow=50, cost=-10)
inpt.connect(otpt)
rec = NumpyArrayNodeRecorder(model, otpt)
model.run()
model.timestepper.end = pandas.to_datetime("2016-01-02")
model.run()
示例11: _fetch_csv
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def _fetch_csv(self, path):
"""
fetch the information and pricetable from path+code.csv, not recommend to use manually,
just set the fetch label to be true when init the object
:param path: string of folder path
"""
try:
content = pd.read_csv(path + self.code + ".csv")
pricetable = content.iloc[1:]
datel = list(pd.to_datetime(pricetable.date))
self.price = pricetable[["netvalue", "totvalue", "comment"]]
self.price["date"] = datel
saveinfo = json.loads(content.iloc[0].date)
if not isinstance(saveinfo, dict):
raise FundTypeError("This csv doesn't looks like from fundinfo")
self.segment = saveinfo["segment"]
self.feeinfo = saveinfo["feeinfo"]
self.name = saveinfo["name"]
self.rate = saveinfo["rate"]
except FileNotFoundError as e:
# print('no saved copy of fund %s' % self.code)
raise e
示例12: get_portfolio_fromttjj
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def get_portfolio_fromttjj(code, start=None, end=None):
startobj = dt.datetime.strptime(start, "%Y%m%d")
endobj = dt.datetime.strptime(end, "%Y%m%d")
if (endobj - startobj).days < 90:
return None # note start is always 1.1 4.1 7.1 10.1 in incremental updates
if code.startswith("F"):
code = code[1:]
r = rget("http://fundf10.eastmoney.com/zcpz_{code}.html".format(code=code))
s = BeautifulSoup(r.text, "lxml")
table = s.find("table", class_="tzxq")
df = pd.read_html(str(table))[0]
df["date"] = pd.to_datetime(df["报告期"])
df["stock_ratio"] = df["股票占净比"].replace("---", "0%").apply(lambda s: _float(s[:-1]))
df["bond_ratio"] = df["债券占净比"].replace("---", "0%").apply(lambda s: _float(s[:-1]))
df["cash_ratio"] = df["现金占净比"].replace("---", "0%").apply(lambda s: _float(s[:-1]))
# df["dr_ratio"] = df["存托凭证占净比"].replace("---", "0%").apply(lambda s: xa.cons._float(s[:-1]))
df["assets"] = df["净资产(亿元)"]
df = df[::-1]
return df[["date", "stock_ratio", "bond_ratio", "cash_ratio", "assets"]]
# this is the most elegant approach to dispatch get_daily, the definition can be such simple
# you actually don't need to bother on start end blah, everything is taken care of by ``cahcedio``
示例13: get_historical_fromhzindex
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def get_historical_fromhzindex(code, start, end):
"""
华证指数源
:param code:
:param start:
:param end:
:return:
"""
if code.startswith("HZ"):
code = code[2:]
r = rget_json(
"http://www.chindices.com/index/values.val?code={code}".format(code=code)
)
df = pd.DataFrame(r["data"])
df["date"] = pd.to_datetime(df["date"])
df = df[["date", "price", "pctChange"]]
df.rename(columns={"price": "close", "pctChange": "percent"}, inplace=True)
df = df[::-1]
return df
示例14: _get_index_weight_range
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def _get_index_weight_range(code, start, end):
if len(code.split(".")) != 2:
code = _inverse_convert_code(code)
start_obj = dt.datetime.strptime(start.replace("-", "").replace("/", ""), "%Y%m%d")
end_obj = dt.datetime.strptime(end.replace("-", "").replace("/", ""), "%Y%m%d")
start_m = start_obj.replace(day=1)
if start_m < start_obj:
start_m = start_m + relativedelta(months=1)
end_m = end_obj.replace(day=1)
if end_obj < end_m:
end_m = end_m - relativedelta(months=1)
d = start_m
df = pd.DataFrame({"code": [], "weight": [], "display_name": [], "date": []})
while True:
if d > end_m:
df["date"] = pd.to_datetime(df["date"])
return df
logger.debug("fetch index weight on %s for %s" % (d, code))
df0 = get_index_weights(index_id=code, date=d.strftime("%Y-%m-%d"))
df0["code"] = df0.index
df = df.append(df0, ignore_index=True, sort=False)
d = d + relativedelta(months=1)
示例15: get_sw_from_jq
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import to_datetime [as 别名]
def get_sw_from_jq(code, start=None, end=None, **kws):
"""
:param code: str. eg. 801180 申万行业指数
:param start:
:param end:
:param kws:
:return:
"""
logger.debug("get sw data of %s" % code)
df = finance.run_query(
query(finance.SW1_DAILY_VALUATION)
.filter(finance.SW1_DAILY_VALUATION.date >= start)
.filter(finance.SW1_DAILY_VALUATION.date <= end)
.filter(finance.SW1_DAILY_VALUATION.code == code)
.order_by(finance.SW1_DAILY_VALUATION.date.asc())
)
df["date"] = pd.to_datetime(df["date"])
return df