Python- TypeError: cannot convert the series to <class 'int'>
Python- TypeError: cannot convert the series to <class 'int'>
我正在编写此代码以转换特定 csv 文件中的列,然后将其转换为人类可读时间。但是,它仅在我指定一个数据值时有效,并且当我尝试转换整列时它显示此错误:
类型错误:无法将系列转换为
import pandas as pd
from tkinter.filedialog import askopenfilename
import csv
from datetime import datetime, timedelta
n=0
file_path= askopenfilename(filetypes=(("CSV File","*.csv"),("All files","*.*")))
print(file_path)
selected_File = pd.read_csv(file_path)
print(selected_File.iloc[:,0])
dtime=datetime.fromtimestamp(selected_File.iloc[:,0])
print(type(dtime))
initial_date="1/1/1900 0:00:00"
i_date= datetime.strptime(initial_date,"%m/%d/%Y %H:%M:%S")
correct_date=i_date+timedelta(days=selected_File.iloc[:,0])
print(correct_date)
您正在调用 Python 标准库函数 datetime.fromtimestamp
,它需要一个整数或类似的东西。您正在传递来自 Pandas 的系列,它不知道如何处理它。
如果要对系列中的每个元素应用一个函数,则需要使用 Series.apply
方法。例如,
selected_File.iloc[0,:].apply(datetime.fromtimestamp)
这是假设数据中的每一列都包含表示时间戳的整数。
您也可以使用 pandas 内置函数 'to_datetime'。
勾选here
我正在编写此代码以转换特定 csv 文件中的列,然后将其转换为人类可读时间。但是,它仅在我指定一个数据值时有效,并且当我尝试转换整列时它显示此错误:
类型错误:无法将系列转换为
import pandas as pd
from tkinter.filedialog import askopenfilename
import csv
from datetime import datetime, timedelta
n=0
file_path= askopenfilename(filetypes=(("CSV File","*.csv"),("All files","*.*")))
print(file_path)
selected_File = pd.read_csv(file_path)
print(selected_File.iloc[:,0])
dtime=datetime.fromtimestamp(selected_File.iloc[:,0])
print(type(dtime))
initial_date="1/1/1900 0:00:00"
i_date= datetime.strptime(initial_date,"%m/%d/%Y %H:%M:%S")
correct_date=i_date+timedelta(days=selected_File.iloc[:,0])
print(correct_date)
您正在调用 Python 标准库函数 datetime.fromtimestamp
,它需要一个整数或类似的东西。您正在传递来自 Pandas 的系列,它不知道如何处理它。
如果要对系列中的每个元素应用一个函数,则需要使用 Series.apply
方法。例如,
selected_File.iloc[0,:].apply(datetime.fromtimestamp)
这是假设数据中的每一列都包含表示时间戳的整数。
您也可以使用 pandas 内置函数 'to_datetime'。
勾选here