我有一个df
时间序列。我提取了索引并希望将它们各自转换为datetime
。你是怎么做的?我尝试使用pandas.to_datetime(x)
,但在使用type()
答案 0 :(得分:46)
>>> import pandas as pd
>>> t = pd.tslib.Timestamp('2016-03-03 00:00:00')
>>> type(t)
pandas.tslib.Timestamp
>>> t.to_datetime()
datetime.datetime(2016, 3, 3, 0, 0)
>>> t.to_pydatetime()
datetime.datetime(2016, 3, 3, 0, 0)
更改为datetime.date
类型
>>> t.date()
datetime.date(2016, 3, 3)
谢谢,@ mjp,to_datetime()
将来会被弃用,请改用to_pydatetime()
!
In [4]: t.to_datetime()
/Users/qiuwei/Library/Python/2.7/lib/python/site-packages/IPython/core/interactiveshell.py:2881: FutureWarning: to_datetime is deprecated. Use self.to_pydatetime()
exec(code_obj, self.user_global_ns, self.user_ns)
Out[4]: datetime.datetime(2016, 3, 3, 0, 0)
答案 1 :(得分:3)
假设您正在尝试转换pandas时间戳对象,您只需从时间戳中提取相关数据:
#Create the data
data = {1: tslib.Timestamp('2013-01-03 00:00:00', tz=None), 2: tslib.Timestamp('2013-01-04 00:00:00', tz=None), 3: tslib.Timestamp('2013-01-03 00:00:00', tz=None)}
#convert to df
df = pandas.DataFrame.from_dict(data, orient = 'index')
df.columns = ['timestamp']
#generate the datetime
df['datetime'] = df['timestamp'].apply(lambda x: datetime.date(x.year,x.month,x.day))
当然,如果你需要秒,分钟和小时,你也可以将它们包括在函数datetime.datetime的参数中。
答案 2 :(得分:3)
我遇到了同样的问题,并尝试了@ aikramer2的解决方案,在我的df类型'datetime.datetime'中添加了一列,但我又得到了一个pandas数据类型:
#libraries used -
import pandas as pd
import datetime as dt
#loading data into a pandas df, from a local file. note column [1] contains a datetime column -
savedtweets = pd.read_csv('/Users/sharon/Documents/ipython/twitter_analysis/conftwit.csv', sep='\t',
names=['id', 'created_at_string', 'user.screen_name', 'text'],
parse_dates={"created_at" : [1]})
print int(max(savedtweets['id'])) #535073416026816512
print type(savedtweets['created_at'][0]) # result is <class 'pandas.tslib.Timestamp'>
# add a column specifically using datetime.datetime library -
savedtweets['datetime'] = savedtweets['created_at'].apply(lambda x: dt.datetime(x.year,x.month,x.day))
print type(savedtweets['datetime'][0]) # result is <class 'pandas.tslib.Timestamp'>
我怀疑pandas df无法存储datetime.datetime数据类型。当我创建一个普通的python列表来存储datetime.datetime值时,我获得了成功:
savedtweets = pd.read_csv('/Users/swragg/Documents/ipython/twitter_analysis/conftwit.csv', sep='\t',
names=['id', 'created_at_string', 'user.screen_name', 'text'],
parse_dates={"created_at" : [1]})
print int(max(savedtweets['id'])) #535073416026816512
print type(savedtweets['created_at'][0]) # <class 'pandas.tslib.Timestamp'>
savedtweets_datetime= [dt.datetime(x.year,x.month,x.day,x.hour,x.minute,x.second) for x in savedtweets['created_at']]
print savedtweets_datetime[0] # 2014-11-19 14:13:38
print savedtweets['created_at'][0] # 2014-11-19 14:13:38
print type(dt.datetime(2014,3,5,2,4)) # <type 'datetime.datetime'>
print type(savedtweets['created_at'][0].year) # <type 'int'>
print type(savedtweets_datetime) # <type 'list'>
答案 3 :(得分:3)
只是问题的更新,我已经尝试了最受欢迎的答案,它给了我这个警告
usr / local / lib / python3.5 / dist-packages / IPython / core / interactiveshell.py:2910:FutureWarning:to_datetime已弃用。使用self.to_pydatetime() exec(code_obj,self.user_global_ns,self.user_ns)
建议我使用to_pydatetime()
例如
sample = Timestamp('2018-05-02 10:08:54.774000')
sample.to_datetime()
将返回datetime.datetime(2018, 4, 30, 10, 8, 54, 774000)
答案 4 :(得分:1)
import time
time.strftime("%H:%M", time.strptime(str(x), "%Y-%m-%d %H:%M:%S"))
注意:x应该是pandas.tslib.Timestamp(因为它在问题中)
答案 5 :(得分:1)
这适用于我,要在MySQL中为filters
创建日期,请尝试:
{
"aggs": {
"ranges": {
"filters": {
"filters": {
"60": {
"bool": {
"must": [
{
"range": {
"min": {
"gte": 60.0
}
}
},
{
"range": {
"max": {
"lt": 60.0
}
}
}
]
}
},
"80": {
"bool": {
"must": [
{
"range": {
"min": {
"gte": 80.0
}
}
},
{
"range": {
"max": {
"lt": 80.0
}
}
}
]
}
}
}
}
}
}
}
答案 6 :(得分:0)
您可以使用to_pydatetime()将Timestamp转换为Python datetime对象,但似乎在将其应用于整个列时,转换受阻:
>>> ts = pd.tslib.Timestamp.now()
>>> type(ts)
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
>>> type(ts.to_pydatetime())
<class 'datetime.datetime'>
>>> df = pd.DataFrame({"now": [datetime.datetime.utcnow()] * 10})
>>> type(df['now'].iloc[0])
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
>>> df['now2'] = df['now'].apply(lambda dt: dt.to_pydatetime())
>>> type(df['now2'].iloc[0])
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
不确定该怎么做。 (在某些情况下,Pandas的Timestamp对象不能完美替代Python的datetime对象,而您想要真实的东西。)
答案 7 :(得分:0)
如果您有两个单独的字段(一个用于日期,一个用于时间),作为替代解决方案:
转换为 datetime.date
df['date2'] = pd.to_datetime(df['date']).apply(lambda x: x.date())
转换为 datetime.time
df['time2'] = pd.to_datetime(df['time']).apply(lambda x: x.time())
之后,您可以组合它们:
df['datetime'] = df.apply(lambda r : pd.datetime.combine(r['date2'],r['time2']),1)
已适应this post
答案 8 :(得分:0)
就我而言,即使指定格式,我也无法获得正确的输出:我以前总是获得1970年。
实际上,解决我问题的方法是为函数指定unit
参数,因为我的时间戳具有秒间隔:
df_new = df
df_new['time'] = pandas.to_datetime(df['time'], unit='s')