Golang中与Python的`pd.to_datetime()`等效的函数是什么?

huangapple go评论72阅读模式
英文:

Golang Equivalent of Python's `pd.to_datetime()`

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学习Go语言,想要创建自己的算法交易策略回测库。在Python方面,我在这个领域有丰富的经验,希望通过这个项目来学习Go语言。

我有一个5分钟的OHLCV数据集SPY5min.csv,数据集的头部如下所示:

Golang中与Python的`pd.to_datetime()`等效的函数是什么?

我使用以下代码从文件中读取数据集,并将其转换为值的列表列表:

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)
}

Golang中与Python的`pd.to_datetime()`等效的函数是什么?

这将返回一个字符串值的列表列表。很好。现在我想要复制一个类似于Pandas Dataframe的对象,或者将每个“列”分别存储在它们自己的数组/切片中,如果这样做更容易的话。

完成这一步之后,我需要一种方法将日期时间的字符串转换为实际的日期时间对象,以便进行比较和loc操作。有人可以指点我吗?

我天真的方法(伪代码)是:

  1. 声明6个数组变量(datetime、open、high、low、close、volume),大小为len(records)
  2. 遍历records的列表列表
  3. 将每个值插入到它们各自数组的当前i位置
  4. 遍历结束后,将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:

Golang中与Python的`pd.to_datetime()`等效的函数是什么?

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)
}

Golang中与Python的`pd.to_datetime()`等效的函数是什么?

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:

  1. Declare 6 array variables (datetime, open, high, low, close, volume) of len(records) in size
  2. Iterate over the records list of lists
  3. Insert each value into the current i of their respective arrays
  4. 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)
}

huangapple
  • 本文由 发表于 2022年11月20日 11:13:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/74505373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定