我在转换时间时遇到问题。 Column[0] 是一个时间戳,我想在 [1] 处插入一个新列,现在它称为 timestamp2。我尝试然后使用 for 语句将列 [0] 转换为可读时间并将其添加到列 [1]。目前我插入了新列,但出现此错误: raise TypeError(f"cannot convert the series to {converter}")
TypeError: cannot convert the series to
def enter_location():
location_list = []
count = 1
Lat1 = float(input("Enter your first LAT coordinate ")
)
Lon1 = float(input("Enter you first LON coordinate ")
)
Loc1 = (Lat1, Lon1)
location_list.append(Loc1)
add_more = input("Enter more coordinates? Y/N ")
if add_more == 'Y':
Lat2 = float(input("Enter your second LAT coordinate ")
)
Lon2 = float(input("Enter you second LON coordinate ")
)
Loc2 = (Lat2, Lon2)
location_list.append(Loc2)
add_more = input("Enter more coordinates? Y/N ")
if add_more == 'Y':
Lat3 = float(input("Enter your third LAT coordinate ")
)
Lon3 = float(input("Enter you third LON coordinate ")
)
Loc3 = (Lat3, Lon3)
location_list.append(Loc3)
add_more = input("Enter more coordinates? Y/N ")
if add_more == 'Y':
Lat4 = float(input("Enter your fourth LAT coordinate ")
)
Lon4 = float(input("Enter you fourth LON coordinate ")
)
Loc4 = (Lat4, Lon4)
location_list.append(Loc4)
add_more = input("Enter more coordinates? Y/N ")
if add_more == 'Y':
Lat5 = float(input("Enter your third LAT coordinate ")
)
Lon5 = float(input("Enter you third LON coordinate ")
)
Loc5 = (Lat5, Lon5)
location_list.append(Loc5)
else:
pass
else:
pass
else:
pass
else:
pass
return location_list
解析数据:
import requests
import json
import pandas as pd
from datetime import datetime
url = 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/cbse/spot/btc-usd/aggregations/count_ohlcv_vwap?interval=1h&page_size=1000'
KEY = 'xxx'
headers = {
"X-Api-Key": KEY,
"Accept": "application/json",
"Accept-Encoding": "gzip"
}
res = requests.get(url, headers=headers)
j_data = res.json()
parse_data = j_data['data']
# create dataframe
df = pd.DataFrame.from_dict(pd.json_normalize(parse_data), orient='columns')
df.insert(1, 'timestamp2', ' ')
for index, row in df.iterrows():
timestamp = df['timestamp'].astype(int)
dt = datetime.fromtimestamp(timestamp)
df.at[index, "timestamp2"] = dt
print(df)
df.to_csv('test.csv', index=False, encoding='utf-8')
在这个例子中,我将 'df.at[index, "timestamp2"] = dt' 设置为 5 只是为了确保它插入每一行,这样做我只需要将 column[0] 转换为可读的时间对于列[1]
答案 0 :(得分:1)
如果将时间戳转换为整数,则根据值的大小,它似乎是自 epoc 以来的毫秒数。
如果您有兴趣,这里有一些关于 unix-time 的更多细节。 https://en.wikipedia.org/wiki/Unix_time
您可以使用 pd.to_datetime 将其转换为日期时间。
这是一个矢量化操作,因此您不需要使用循环遍历数据帧。 pd.to_numeric 和 pd.to_datetime 都可以应用于整个系列。
没有你的所有数据很难调试,但下面应该可以工作。 .astype(int) 是 pd.to_numeric 的替代方法,唯一的区别是 pd.to_numeric 为您提供了更大的错误处理灵活性,允许您强制转换为 nan(不确定是否需要)。
import pandas as pd
df = pd.DataFrame({'timestamp':['1611169200000']})
# convert to integer. If there are invalid entries this will set to nan. Depends on your case how you want to treat these.
timestamp_num = pd.to_numeric(df['timestamp'],errors='ignore')
df['timestamp2'] pd.to_datetime(timestamp_num,unit='ms')
print(df.to_dict())
#{'timestamp': {0: '1611169200000'}, 'timestamp2': {0: Timestamp('2021-01-20 19:00:00')}}