英文:
Golang Equivalent of Python's `pd.to_datetime()`
问题
我是你的中文翻译助手,以下是翻译好的内容:
我刚开始学习Go语言,想要创建自己的算法交易策略回测库。在Python方面,我在这个领域有丰富的经验,希望通过这个项目来学习Go语言。
我有一个5分钟的OHLCV数据集SPY5min.csv
,数据集的头部如下所示:
我使用以下代码从文件中读取数据集,并将其转换为值的列表列表:
package main
import (
"encoding/csv"
"log"
"os"
"fmt"
)
func ReadCsvFile(filePath string) [][]string {
f, err := os.Open(filePath)
if err != nil {
log.Fatal("无法读取输入文件 "+filePath, err)
}
defer f.Close()
csvReader := csv.NewReader(f)
records, err := csvReader.ReadAll()
if err != nil {
log.Fatal("无法将文件解析为CSV格式 "+filePath, err)
}
return records
}
func main() {
records := ReadCsvFile("./SPY5min.csv")
fmt.Println(records)
}
这将返回一个字符串值的列表列表。很好。现在我想要复制一个类似于Pandas Dataframe的对象,或者将每个“列”分别存储在它们自己的数组/切片中,如果这样做更容易的话。
完成这一步之后,我需要一种方法将日期时间的字符串转换为实际的日期时间对象,以便进行比较和loc
操作。有人可以指点我吗?
我天真的方法(伪代码)是:
- 声明6个数组变量(datetime、open、high、low、close、volume),大小为
len(records)
- 遍历
records
的列表列表 - 将每个值插入到它们各自数组的当前
i
位置 - 遍历结束后,将
datetime
数组中的值批量转换为日期时间对象的值?
想知道这是否真的是最好的方法,或者是否有比O(n)
迭代更快的方法?
英文:
I'm new to Go, and looking to create my own algo trading strategy backtesting library, an area I'm well experienced in with Python, to help learn the language.
I have a 5 minute OHLCV SPY5min.csv
dataset, the head of which looks like this:
I use this code to read in the dataset from the file, converting everything to a list of lists of values:
package main
import (
"encoding/csv"
"log"
"os"
"fmt"
)
func ReadCsvFile(filePath string) [][]string {
f, err := os.Open(filePath)
if err != nil {
log.Fatal("Unable to read input file "+filePath, err)
}
defer f.Close()
csvReader := csv.NewReader(f)
records, err := csvReader.ReadAll()
if err != nil {
log.Fatal("Unable to parse file as CSV for "+filePath, err)
}
return records
}
func main() {
records := ReadCsvFile("./SPY5min.csv")
fmt.Println(records)
}
This returns a list of lists of string values. Cool. Now what I want to do is replicate a Pandas Dataframe like object, or perhaps separate each "column" into their own separate arrays/slices if that's easier, not sure yet.
Once that's done, I need a way to convert the strings of datetimes to actual datetime objects that I can run comparisons and loc
's on. Can someone point me in the right direction?
My naive approach (pseudo) would be to:
- Declare 6 array variables (datetime, open, high, low, close, volume) of
len(records)
in size - Iterate over the
records
list of lists - Insert each value into the current
i
of their respective arrays - Once iteration is done, mass convert the values in the
datetime
array to values of datetime objects?
Wondering if this is really the best way of doing this, or if there's a faster way than O(n)
iteration?
答案1
得分: 2
你问道:“完成后,我需要一种将日期时间字符串转换为实际的日期时间对象的方法...”。我最近在这里回答了一个类似的问题:https://stackoverflow.com/a/74491722/5739452
你的时间戳看起来像这样:"2022-11-08 4:00"
。time
包包含了解析和其他操作函数。关键细节是了解布局解析器格式的约定。时间的每个元素都被识别为一个特定的数字。年份是 2006
,月份是 01
等等。
所以,对于你的目的,可以使用类似以下的代码:
package main
import (
"fmt"
"time"
)
func main() {
t := "2022-11-08 4:00"
const layout = "2006-01-02 15:04"
x, err := time.Parse(layout, t)
fmt.Println(x, err)
}
英文:
You asked, "Once that's done, I need a way to convert the strings of datetimes to actual datetime objects ...". I recently answered a similar question here: https://stackoverflow.com/a/74491722/5739452
Your timestamp looks like this: "2022-11-08 4:00"
. The time
package contains parsing and other manipulation functions. The key detail is knowing the conventions for the layout parser format. Each element of a time is recognized as a specific number. The year is 2006
, the month is 01
etc.
So, for your purpose something like this should work:
package main
import (
"fmt"
"time"
)
func main() {
t := "2022-11-08 4:00"
const layout = "2006-01-02 15:04"
x, err := time.Parse(layout, t)
fmt.Println(x, err)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论