mfmarb
mfmarb

Reputation: 65

Python- TypeError: cannot convert the series to <class 'int'>

I am writing this code to convert the column in a specific csv file and then convert it to human readable time. However, it works only if I specified one data value and when I try to convert the whole column it shows this error: TypeError: cannot convert the series to <class 'int'>

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) 

Upvotes: 0

Views: 1889

Answers (2)

Kantajit
Kantajit

Reputation: 418

You can use pandas inbuilt function 'to_datetime' also.

Check here

Upvotes: 2

Iguananaut
Iguananaut

Reputation: 23346

You are calling the Python standard library function datetime.fromtimestamp which expects an integer or something like it. You are passing a Series from Pandas, which it has no idea what to do with.

If you want to apply a function for every element in a series, you need to use the Series.apply method. E.g.,

selected_File.iloc[0,:].apply(datetime.fromtimestamp)

This is assuming that every column in your data contains integers representing timestamps.

Upvotes: 3

Related Questions