有没有一种方法可以使用Python或Matlab将csv数据转换为Matlab时间表?

inkz8wg9  于 2023-05-26  发布在  Python
关注(0)|答案(3)|浏览(155)

我想知道如何从这些数据制作Matlab时间表:

date   time     open     high      low    close tickvol
0        2011.01.02  17:01   1.3342   1.3344   1.3341   1.3341       0
1        2011.01.02  17:02   1.3343   1.3343   1.3343   1.3343       0
2        2011.01.02  17:03   1.3344   1.3344   1.3344   1.3344       0
3        2011.01.02  17:06   1.3345   1.3349   1.3345   1.3348       0
4        2011.01.02  17:07   1.3346   1.3346   1.3346   1.3346       0
...             ...    ...      ...      ...      ...      ...     ...
4558998  2023.05.19  16:54  1.08051  1.08057  1.08046  1.08046       0
4558999  2023.05.19  16:55  1.08047  1.08055  1.08042  1.08054       0
4559000  2023.05.19  16:56  1.08053  1.08058   1.0805  1.08055       0
4559001  2023.05.19  16:57  1.08055   1.0806  1.08048  1.08056       0
4559002  2023.05.19  16:58  1.08055  1.08057  1.08055  1.08057       0

不太了解matlab,也不知道用python做什么来留下正确的数据。
我能做些什么来实现这一点?
编辑:
我试过这个:

TT=readtimetable('EURUSD_big_data.csv')

但出现此错误:

Expected a datetime or duration variable to use as RowTimes. Check that the data can
be converted to datetime or duration, or supply a "TimeStep" or "SampleRate".

如何使用

TimeStep

在这个例子中?

zpqajqem

zpqajqem1#

既然你有一个看起来很大的pandasDataFrame,你可以使用mfinance。我将绘制一个在文档中找到的数据集(* 与您的格式匹配,但有更多的行来选择一段时间--通过使用部分字符串索引 *--),以获得有意义的可视化。

import pandas as pd
import mplfinance as mpf #pip install mplfinance

url = "https://raw.githubusercontent.com/matplotlib/" \
      "mplfinance/master/examples/data/SP500_NOV2019_IDay.csv"

df = pd.read_csv(url, index_col=0, parse_dates=True)

a_period = df.loc["2019-11-06 15:00":"2019-11-06 16:00", :]

mpf.plot(a_period, type="candle", mav=(7,12))

输出:

  • 使用的数据集/周期:*
print(a_period)

                       Open   Close    High     Low  Volume
Date                                                       
2019-11-06 15:00:00 3075.25 3074.57 3075.25 3074.57       0
2019-11-06 15:01:00 3074.55 3074.49 3074.70 3074.30       0
2019-11-06 15:02:00 3074.48 3074.24 3074.56 3074.21       0
...                     ...     ...     ...     ...     ...
2019-11-06 15:58:00 3075.49 3075.54 3075.98 3075.49       0
2019-11-06 15:59:00 3075.69 3076.75 3076.79 3075.55       0
2019-11-06 16:00:00 3076.74 3076.73 3076.74 3076.72       0

[61 rows x 5 columns]
r6hnlfcb

r6hnlfcb2#

在matlab中,可以通过使用

table2timetable()

首先,你需要在python中创建一个DataFrame,将time设置为date time:

data["Time"]= pd.to_datetime(data["Time"],unit='s')

在这之后,你必须索引时间

data.set_index(data["Time"],inplace=True)

并将df传递给csv

data.to_csv(str(symbol)+".csv")

一旦你有了CSV,在MATLAB中
读取csv并使其成为表

prices=readtable("EURUSD.csv")

把table整理好

prices=table2timetable(prices)

删除NaN行(& D)

prices=rmmissing(prices)
busg9geu

busg9geu3#

我不认为这件事有太多的复杂性。编写一个带有时间列的CSV文件并将其读入matlab
https://www.mathworks.com/help/matlab/ref/readtimetable.html(可能会在某个时间死亡...)
编辑:
接受你所尝试的。你已经OUTPUT了dataframe的index列。这是作为你的日期时间列读到时间表。手动删除该列(更痛苦),或者更改输出代码,使其不保存索引列--我认为这是错误的。
您可能还需要使用datetime库或类似的库将日期和时间列组合成datetime列-或作为时间戳/epochs。
还要注意的是,table 2 timestimate不能解决你的问题,因为你没有一个列可以作为采样率、时间戳、日期时间等。您必须首先修复数据集输出格式,然后才能修复使用

readtimetable('file.csv')

table2timetabel('file.csv', 'RowTimes', timeVarName)

相关问题