[英]Convert Representation of datetime.date to Representation of pandas.Timestamp
I've converted a bunch of code from using datetime.date
objects to using Timestamps
. 我已经将一堆代码从使用
datetime.date
对象转换为使用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)". 代码由大量单元测试覆盖,这意味着我需要将所有实例(如
"datetime.date(2016, 12, 20)"
为“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. 问题是
date
使用一个或两个数字来显示月份和日期,而Timestamp
总是使用两个。 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)"
. 因此,如果日期是
datetime.date(2016, 1, 1)
"Timestamp(2016-1-1)
datetime.date(2016, 1, 1)
我将返回"Timestamp(2016-1-1)
”,但正确的表示应该是"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? 有没有办法可以使用
re.sub()
来进行这种转换?
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
. 使用宽度为
2
zfill
。
you can use a combination of pd.to_datetime
and eval
您可以使用
pd.to_datetime
和eval
的组合
make sure you import datetime
to get eval to work. 确保导入
datetime
以使eval工作。
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(
: 为什么不能简单地替换
datetime.date(
使用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 : pd.Timestamp docstring :
TimeStamp is the pandas equivalent of python's Datetime and is interchangable with it in most cases .
TimeStamp是大熊猫相当于python的Datetime,并且 在大多数情况下可以与它互换 。 It's the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas.
它是用于构成DatetimeIndex的条目的类型,以及用于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.
参数---------- ts_input:datetime-like,str,int,float要转换为Timestamp的值freq:str,DateOffset Offset哪个Timestamp将具有tz:string,pytz.timezone,dateutil.tz。 tzfile或无Timestamp将具有的时间区域。 unit : string numpy unit used for conversion, if ts_input is int or float offset : str, DateOffset Deprecated, use freq
unit:string numpy unit用于转换,如果ts_input是int或float offset:str,DateOffset已弃用,请使用freq
The other two forms mimic the parameters from
datetime.datetime
.另外两种形式模仿
datetime.datetime
的参数。 They can be passed by either position or keyword, but not both mixed together.它们可以通过位置或关键字传递,但不能两者混合在一起。
:func:
datetime.datetime
Parameters:func:
datetime.datetime
参数.. versionadded:: 0.19.0
.. 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
year:int month:int day:int hour:int,optional,默认为0分钟:int,可选,默认为0秒:int,可选,默认为0微秒:int,可选,默认为0 tzinfo:datetime.tzinfo ,可选,默认为None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.