如何将时间列转换为 Unix 时间戳?
How to convert time column to Unix timestamp?
我想将 Datetime 列转换为 Epoch/Unix 时间戳。
import pandas as pd
import yfinance as yf
import numpy as np
import time
df = yf.download(tickers='^NSEI',period='1d',interval='15m')
df.reset_index(inplace=True)
df.rename(columns = {'Datetime':'time'}, inplace = True)
df['Date'] = df['time'].dt.strftime('%Y-%m-%d')
df['time'] = df['time'].dt.strftime("%Y-%m-%d %H:%M:%S")
df
使用此提供功能citation
def utctimestamp(ts: str, DATETIME_FORMAT: str = "%d/%m/%Y"):
import datetime, calendar
ts = datetime.datetime.utcnow() if ts is None else datetime.datetime.strptime(ts, DATETIME_FORMAT)
return calendar.timegm(ts.utctimetuple())
示例代码(未测试)
# Imports
import datetime, calendar
# custom function
def utctimestamp(ts: str, DATETIME_FORMAT: str = "%d/%m/%Y %H:%M:%S"):
ts = datetime.datetime.utcnow() if ts is None else datetime.datetime.strptime(ts, DATETIME_FORMAT)
return calendar.timegm(ts.utctimetuple())
# updated code
df = yf.download(tickers='^NSEI',period='1d',interval='15m')
df.reset_index(inplace=True)
df.rename(columns = {'Datetime':'time'}, inplace = True)
df['Date'] = df['time'].dt.strftime('%Y-%m-%d')
df['time'] = utctimestamp(df['time'].dt.strftime("%Y/%m/%d %H:%M:%S"))
我想将 Datetime 列转换为 Epoch/Unix 时间戳。
import pandas as pd
import yfinance as yf
import numpy as np
import time
df = yf.download(tickers='^NSEI',period='1d',interval='15m')
df.reset_index(inplace=True)
df.rename(columns = {'Datetime':'time'}, inplace = True)
df['Date'] = df['time'].dt.strftime('%Y-%m-%d')
df['time'] = df['time'].dt.strftime("%Y-%m-%d %H:%M:%S")
df
使用此提供功能citation
def utctimestamp(ts: str, DATETIME_FORMAT: str = "%d/%m/%Y"):
import datetime, calendar
ts = datetime.datetime.utcnow() if ts is None else datetime.datetime.strptime(ts, DATETIME_FORMAT)
return calendar.timegm(ts.utctimetuple())
示例代码(未测试)
# Imports
import datetime, calendar
# custom function
def utctimestamp(ts: str, DATETIME_FORMAT: str = "%d/%m/%Y %H:%M:%S"):
ts = datetime.datetime.utcnow() if ts is None else datetime.datetime.strptime(ts, DATETIME_FORMAT)
return calendar.timegm(ts.utctimetuple())
# updated code
df = yf.download(tickers='^NSEI',period='1d',interval='15m')
df.reset_index(inplace=True)
df.rename(columns = {'Datetime':'time'}, inplace = True)
df['Date'] = df['time'].dt.strftime('%Y-%m-%d')
df['time'] = utctimestamp(df['time'].dt.strftime("%Y/%m/%d %H:%M:%S"))