gpt4 book ai didi

python - 在 Python 中使用 pandas 和 datetime 从字符串中获取时间信息

转载 作者:太空宇宙 更新时间:2023-11-03 12:04:00 26 4
gpt4 key购买 nike

我正在为暑期学校做测试练习,这是我第一次使用 Python。据说,我可以通过以下代码获取我的时间信息:

import pandas as pd
from datetime import datetime

def print_time_range(times):
print('Time from: {0}, to: {1}'.format(datetime.fromtimestamp(times.min()), datetime.fromtimestamp(times.max())))

train_likes_df = pd.read_csv('train_likes.csv')
print train_likes_df.shape
train_likes_df.head(3)
print_time_range(train_likes_df.time)

这将给我输出:

Time from: 2014-01-10 17:15:37, to: 2016-02-24 15:15:37.

来自类似这样的时间元素:1389733974.0。

但是我得到了 pandas 的属性错误,声称没有这样的属性 time error我知道,time 是数据时间的方法,不能与 DataFrame 一起使用,但我只知道这些。我怎样才能使一切正常? CSV file

最佳答案

我想你需要dt.time :

print_time_range(train_likes_df.time.dt.time)

你需要to_datetime带参数 unit:

train_likes_df['time'] = pd.to_datetime(train_likes_df['time'], unit='s')

示例:

import pandas as pd
import io

temp=u"""user_id,item_id,channel,time
aa,bb,cc,1389733974
aa,bb,cc,1390459377"""
#after testing replace io.StringIO(temp) to filename
train_likes_df = pd.read_csv(io.StringIO(temp))

print (train_likes_df)
user_id item_id channel time
0 aa bb cc 1389733974
1 aa bb cc 1390459377

train_likes_df['time'] = pd.to_datetime(train_likes_df['time'], unit='s')
print (train_likes_df)
user_id item_id channel time
0 aa bb cc 2014-01-14 21:12:54
1 aa bb cc 2014-01-23 06:42:57

print (train_likes_df.time.dt.time)
0 21:12:54
1 06:42:57
Name: time, dtype: object

你的函数运行良好:

from datetime import datetime
def print_time_range(times):
print ('Time from: {0}, to: {1}'.format(datetime.fromtimestamp(times.min()), datetime.fromtimestamp(times.max())))

print_time_range(train_likes_df.time)
Time from: 2014-01-14 22:12:54, to: 2014-01-23 07:42:57

如果有键盘错误,可能有一些空间,可以通过print (train_likes_df.columns)检查 - time 列有问题 - 有 空格:

import pandas as pd
import io

temp=u"""user_id,item_id,channel,time ,test
aa,bb,cc,1389733974,1
aa,bb,cc,1390459377,2"""
#after testing replace io.StringIO(temp) to filename
train_likes_df = pd.read_csv(io.StringIO(temp))

print (train_likes_df)
user_id item_id channel time test
0 aa bb cc 1389733974 1
1 aa bb cc 1390459377 2

print (train_likes_df.columns)
Index(['user_id', 'item_id', 'channel', 'time ', 'test'], dtype='object')

您可以通过 strip 删除空格:

train_likes_df.columns = train_likes_df.columns.str.strip()

print (train_likes_df.columns)
Index(['user_id', 'item_id', 'channel', 'time', 'test'], dtype='object')

按文件编辑(我添加 .head() 以仅过滤前 5 条记录以获得更好的输出):

import pandas as pd

train_likes_df = pd.read_csv('train_likes.csv')
train_likes_df['time'] = pd.to_datetime(train_likes_df['time'], unit='s')

print (train_likes_df.head())
user_id item_id \
0 612d8e8eef05acff3278c061ec10f704 7aa5d00445cb9d61d1739dd0df9a0a88
1 71a7f1d1be96603971ba66e4a17e845c 5edaf734b432e5cc954a10b59cb97e70
2 6eaa117728d50265e6b2ac24a80e04ae 8ad97d075fce19c2d182eb2a4539aa1c
3 5d9db6ab742755197343505bccfad516 aa5f2ca699da42e467e550f9f071fb3f
4 3000a163610654f1fa181e74136d2d35 8142d0e687c1c7a317ed9673db9f11a4

channel time
0 1f0e3dad99908345f7439f8ffabdffc4 2014-01-14 21:12:54
1 ec5decca5ed3d6b8079e2e7e7bacc9f2 2014-01-23 06:42:57
2 98f13708210194c475687be6106a3b84 2014-01-30 06:39:23
3 98f13708210194c475687be6106a3b84 2014-01-30 03:36:17
4 c74d97b01eae257e44aa9d5bade97baf 2014-02-14 12:30:23

print (train_likes_df.time.dt.time.head())
0 21:12:54
1 06:42:57
2 06:39:23
3 03:36:17
4 12:30:23
Name: time, dtype: object

关于python - 在 Python 中使用 pandas 和 datetime 从字符串中获取时间信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38271232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com