[英]how to transform string date format using iso 8601 and week numbers
I have this:我有这个:
import pandas as pd
data = pd.DataFrame({'Date': ['2019-01-30', '2019-12-31', '2018-12-31']})
data['Date'] = pd.to_datetime(data.Date)
data['Period'] = data.Date.dt.strftime('Y%G-W%V')
data['Period'] = data['Period'] + '-7'
pd.to_datetime(data.Period, format='Y%G-W%V-%u')
and I have this error (edit):我有这个错误(编辑):
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~/ph_test/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in _convert_listlike(arg, box, format, name, tz)
376 try:
--> 377 values, tz = conversion.datetime_to_datetime64(arg)
378 return DatetimeIndex._simple_new(values, name=name, tz=tz)
pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.datetime_to_datetime64()
TypeError: Unrecognized value type: <class 'str'>
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-1-963014b2e6bc> in <module>
4 data['Period'] = data.Date.dt.strftime('Y%G-W%V')
5 data['Period'] = data['Period'] + '-7'
----> 6 pd.to_datetime(data.Period, format='Y%G-W%V-%u')
~/ph_test/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, box, format, exact, unit, infer_datetime_format, origin, cache)
449 else:
450 from pandas import Series
--> 451 values = _convert_listlike(arg._values, True, format)
452 result = Series(values, index=arg.index, name=arg.name)
453 elif isinstance(arg, (ABCDataFrame, MutableMapping)):
~/ph_test/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in _convert_listlike(arg, box, format, name, tz)
378 return DatetimeIndex._simple_new(values, name=name, tz=tz)
379 except (ValueError, TypeError):
--> 380 raise e
381
382 if arg is None:
~/ph_test/lib/python3.7/site-packages/pandas/core/tools/datetimes.py in _convert_listlike(arg, box, format, name, tz)
345 try:
346 result = array_strptime(arg, format, exact=exact,
--> 347 errors=errors)
348 except tslib.OutOfBoundsDatetime:
349 if errors == 'raise':
pandas/_libs/tslibs/strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()
pandas/_libs/tslibs/strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()
ValueError: 'G' is a bad directive in format 'Y%G-W%V-%u'
any idea, basically I want to use week numbers in iso 8601, Monday as starting day of the week.任何想法,基本上我想使用iso 8601中的周数,星期一作为一周的开始日。
I am using:我在用:
data.loc[:, 'end_date'] = data.Period.apply(lambda x:
datetime.strptime(x + 'd7', 'Y%GW%Vd%u'))
but I have a lot of data and takes a lot of time但是我有很多数据并且需要很多时间
In the comment:在评论中:
i need the last day of this week – Náthali Apr 1 at 11:51
我需要本周的最后一天 – Nathali 4 月 1 日 11:51
You might want to take a look at the metomi-isodatetime
package你可能想看看
metomi-isodatetime
包
from metomi.isodatetime.parsers import TimePointParser
DATA = ['2019-01-30', '2019-12-31', '2018-12-31', '2020-08-13']
tparser = TimePointParser()
lastdays = []
for tp in (tparser.parse(d) for d in DATA):
y, w, _ = tp.get_week_date()
y, m, d = tparser.parse(f"{y}W{w:02}7").get_calendar_date()
lastdays.append(f"{y}-{m:02}-{d:02}")
print(lastdays)
The above returns:以上返回:
['2019-02-03', '2020-01-05', '2019-01-06', '2020-08-16']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.