pandas - 在 Pandas 中将我自己的日期分配给格式为 HH:MM:SSS.000 的日期列
问题描述
我将数据记录到一个 csv 文件中,该文件具有 HH:MM:SSS.000 形式的时间图。 原始数据图像。当我通过这行代码将这些数据读入熊猫时,它会自动将今天的日期添加到 parse_dates 函数的列中;
import pandas as pd
df = pd.read_csv('20-9-2019-ETH.csv', names=['Volume', 'Price', 'Time'],
index_col=2, parse_dates=True)
df.head()
Volume Price
Time
2020-03-01 00:00:11.904 0.091683 217.60
2020-03-01 00:00:12.730 0.916826 217.60
2020-03-01 00:00:12.430 0.331441 217.60
2020-03-01 00:00:15.161 1.420000 217.59
2020-03-01 00:00:15.354 0.174274 217.57
我需要做什么才能指定应该使用的日期而不是创建文件的日期?或者,也许我可以完全删除日期,只保留时间戳?两种解决方案都可以,或者两者都可以,所以我可以了解更多!谢谢!
解决方案
一个相当直接的解决方案是将数据读取为字符串,然后使用pandas.to_timedelta
.
示例代码:
from io import StringIO
import pandas as pd
raw_data = \
"""
col_1,col_2
a val,00:00:11.904
another val,00:00:12.730
a third val,00:00:12.430
fourth val,00:00:15.161
fifth val,00:00:15.354
"""
df = pd.read_csv(StringIO(raw_data), header=0, dtype={"col_1": "string", "col_2": "string"})
print(f"{df}\n\n{df.dtypes}\n\n")
df["col_2"] = pd.to_timedelta(df["col_2"])
print(f"{df}\n\n{df.dtypes}")
输出:
col_1 col_2
0 a val 00:00:11.904
1 another val 00:00:12.730
2 a third val 00:00:12.430
3 fourth val 00:00:15.161
4 fifth val 00:00:15.354
col_1 object
col_2 string
dtype: object
col_1 col_2
0 a val 00:00:11.904000
1 another val 00:00:12.730000
2 a third val 00:00:12.430000
3 fourth val 00:00:15.161000
4 fifth val 00:00:15.354000
col_1 object
col_2 timedelta64[ns]
dtype: object
推荐阅读
- amazon-web-services - 在 Amazon SNS 控制面板中创建平台应用程序的选项在哪里?
- python - Python:使用 subprocess.Popen 执行一个长时间运行的进程,将其杀死并访问其输出
- symfony - Symfony - 记录到服务中的多个日志文件
- git - 远程存储库中的 .gitignore 项目会被 git clone 复制到本地项目存储库中吗?
- java - Spring boot Cloud Embedding Config Server 从 Git repo 获取每个请求的属性
- php - 在 Woocommerce 中只允许为特定产品类别购买一种产品
- azure - 在未显示的循环中列出 blob
- node.js - MongoDB 搜索在某些情况下失败
- swift - 从 Swift 中的 webView 获取 html 内容?
- javascript - JavaScript:如果字段已填充,则启用按钮不是自动的