I've converted a bunch of code from using datetime.date
objects to using Timestamps
. The code is covered by a ton of unit tests, which means that I need to convert all the instances like "datetime.date(2016, 12, 20)"
to `"Timestamp(2016-12-20)".
The easy solution is:
re.sub(r"datetime.date\((\d{4}), (\d{1,2}), (\d{1,2})\)", r"Timestamp(\1-\2-\3)", string)
Which works fine in some cases. The problem is that date
uses one or two digits to display the month and day, whereas Timestamp
always uses two. So if the date was datetime.date(2016, 1, 1)
I'd get back "Timestamp(2016-1-1)
" but the correct representation should be "Timestamp(2016-01-01)"
.
Some of the string instances also contain multiple substring matches.
Is there a way that I can use re.sub()
to do this conversion?
string = "datetime.date(2016, 2, 20)"
def repl(matchobj):
return "Timestamp(%s-%s-%s)"%(matchobj.group(1), matchobj.group(2).zfill(2), matchobj.group(3).zfill(2))
print re.sub(r"datetime.date\((\d{4}), (\d{1,2}), (\d{1,2})\)", repl, string)
Output:
Timestamp(2016-02-20)
Use zfill
with width 2
.
you can use a combination of pd.to_datetime
and eval
make sure you import datetime
to get eval to work.
import datetime
import pandas as pd
pd.to_datetime(eval("datetime.date(2016, 3, 31)"))
Timestamp('2016-03-31 00:00:00')
why can't you simply replace datetime.date(
with pd.Timestamp(
:
In [26]: datetime.date(2000,1,30)
Out[26]: datetime.date(2000, 1, 30)
In [27]: pd.Timestamp(2000,1,30)
Out[27]: Timestamp('2000-01-30 00:00:00')
In [28]: datetime.date(2000,1,3)
Out[28]: datetime.date(2000, 1, 3)
In [29]: pd.Timestamp(2000,1,3)
Out[29]: Timestamp('2000-01-03 00:00:00')
RegEx:
re.sub(r'datetime.date\s*\(', r'pd.Timestamp(', string)
pd.Timestamp docstring :
TimeStamp is the pandas equivalent of python's Datetime and is interchangable with it in most cases . It's the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas.
There are essentially three calling conventions for the constructor. The primary form accepts four parameters. They can be passed by position or keyword.
Parameters ---------- ts_input : datetime-like, str, int, float Value to be converted to Timestamp freq : str, DateOffset Offset which Timestamp will have tz : string, pytz.timezone, dateutil.tz.tzfile or None Time zone for time which Timestamp will have. unit : string numpy unit used for conversion, if ts_input is int or float offset : str, DateOffset Deprecated, use freq
The other two forms mimic the parameters from
datetime.datetime
. They can be passed by either position or keyword, but not both mixed together.:func:
datetime.datetime
Parameters.. versionadded:: 0.19.0
year : int month : int day : int hour : int, optional, default is 0 minute : int, optional, default is 0 second : int, optional, default is 0 microsecond : int, optional, default is 0 tzinfo : datetime.tzinfo, optional, default is None
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.