gpt4 book ai didi

python - 将日期时间转换为自 UTC 午夜以来的毫秒数或使用 Pandas 在 CSV 文件中进行本地化

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

import pandas as pd
import numpy as np
from datetime import datetime, time


# history file and batch size for processing.

historyFilePath = 'EURUSD.SAMPLE.csv'
batch_size = 5000


# function for date parsing
dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f')


# load data into a pandas iterator with all the chunks
ratesFromCSVChunks = pd.read_csv(historyFilePath, index_col=0, engine='python', parse_dates=True,
date_parser=dateparse, header=None,
names=["datetime", "1_Current", "2_BidPx", "3_BidSz", "4_AskPx", "5_AskSz"],
iterator=True,
chunksize=batch_size)



# concatenate chunks to get the final array
ratesFromCSV = pd.concat([chunk for chunk in ratesFromCSVChunks])

# save final csv file
df.to_csv('EURUSD_processed.csv', date_format='%Y-%m-%d %H:%M:%S.%f',
columns=['1_Current', '2_BidPx', '3_BidSz', '4_AskPx', '5_AskSz'], header=False, float_format='%.5f')

我正在读取一个包含格式为

的外汇数据的 CSV 文件
    2014-08-17 17:00:01.000000,1.33910,1.33910,1.00000,1.33930,1.00000
2014-08-17 17:00:01.000000,1.33910,1.33910,1.00000,1.33950,1.00000
2014-08-17 17:00:02.000000,1.33910,1.33910,1.00000,1.33930,1.00000
2014-08-17 17:00:02.000000,1.33900,1.33900,1.00000,1.33940,1.00000
2014-08-17 17:00:04.000000,1.33910,1.33910,1.00000,1.33950,1.00000
2014-08-17 17:00:05.000000,1.33930,1.33930,1.00000,1.33950,1.00000
2014-08-17 17:00:06.000000,1.33920,1.33920,1.00000,1.33960,1.00000
2014-08-17 17:00:06.000000,1.33910,1.33910,1.00000,1.33950,1.00000
2014-08-17 17:00:08.000000,1.33900,1.33900,1.00000,1.33942,1.00000
2014-08-17 17:00:16.000000,1.33900,1.33900,1.00000,1.33940,1.00000

如何在保存时将正在读取的 CSV 文件或 pandas 数据帧中的数据时间从 MIDNIGHT(UTC 或本地化)转换为以 MILLISECONDS 为单位的 EPOCH 时间。每个文件每天午夜开始。唯一改变的是日期时间的格式,从每天午夜开始以毫秒为单位(UTC 或本地化)。我正在寻找的格式是:

    43264234, 1.33910,1.33910,1.00000,1.33930,1.00000
43264739, 1.33910,1.33910,1.00000,1.33950,1.00000
43265282, 1.33910,1.33910,1.00000,1.33930,1.00000
43265789, 1.33900,1.33900,1.00000,1.33940,1.00000
43266318, 1.33910,1.33910,1.00000,1.33950,1.00000
43266846, 1.33930,1.33930,1.00000,1.33950,1.00000
43267353, 1.33920,1.33920,1.00000,1.33960,1.00000
43267872, 1.33910,1.33910,1.00000,1.33950,1.00000
43268387, 1.33900,1.33900,1.00000,1.33942,1.00000

非常感谢任何帮助(在 Python 3.5 或 Python 3.4 及更高版本中使用 Pandas 0.18.1 和 numpy 1.11 时简短而精确)

最佳答案

这段代码应该是你想要的

# Create some fake data, similar to yours

import pandas as pd
s = pd.Series(pd.date_range('2014-08-17 17:00:01.1230000', periods=4))
print(s)
print(type(s[0]))

# Create a new series using just the date portion of the original data.
# This effectively truncates the time portion.
# Can't use d = s.dt.date or you'll get date objects back, not datetime64.

d = pd.to_datetime(s.dt.date)
print(d)
print(type(d[0]))

# Calculate the time delta between the original datetime and
# just the date portion. This is the elapsed time since your epoch.

delta_t = s-d
print(delta_t)

# Display the elapsed time as seconds.

print(delta_t.dt.total_seconds())

这会导致以下输出

0   2014-08-17 17:00:01.123
1 2014-08-18 17:00:01.123
2 2014-08-19 17:00:01.123
3 2014-08-20 17:00:01.123
dtype: datetime64[ns]
<class 'pandas.tslib.Timestamp'>
0 2014-08-17
1 2014-08-18
2 2014-08-19
3 2014-08-20
dtype: datetime64[ns]
<class 'pandas.tslib.Timestamp'>
0 17:00:01.123000
1 17:00:01.123000
2 17:00:01.123000
3 17:00:01.123000
dtype: timedelta64[ns]
0 61201.123
1 61201.123
2 61201.123
3 61201.123
dtype: float64

关于python - 将日期时间转换为自 UTC 午夜以来的毫秒数或使用 Pandas 在 CSV 文件中进行本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38044264/

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