Cannot Convert the Series to <Class 'Int''>

Cannot convert the series to <class 'int'`>

Your error is on line 2. df['intage'] = int(df['age']) is not valid, and you can't pass a pandas series to the int function.

You need to use astype if df['age'] is object dtype.

df['intage'] = df['age'].astype(int)

Or since you are subtracting two dates, you need to use the dt accessor with the days attribute to get the number of days as an integer:

df['intage'] = df['age'].dt.days

Python TypeError: cannot convert the series to <class 'int'> when trying to do math on dataframe

What if you do this (as was suggested earlier):

new_time = dfs['XYF']['TimeUS'].astype(float)
new_time_F = new_time / 1000000

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

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.

Change several columns of a dataframe to integer type. Error cannot convert the series to <class 'int'>

You should change this line of code to achieve the desired result.

df[cols] = df[cols].apply(lambda a:a.astype(int),axis=1)

Cannot Convert the series to <class 'int'>?

There are many issues here that your data.csv have the format (that you provided in a comments)... note that you need to use ' ' (a space) as the delimiter in pandas read_csv() function, because the data in your .csv is space separated and not comma separated:

$ cat data.csv
2012-08-02 13:10:00 66.4
2012-08-02 13:39:00 33.4
2012-08-03 13:39:00 66.8
2012-08-03 17:39:00 39.1

Now lets load the csv data into pandas using read_csv():

import pandas as pd
data = pd.read_csv('data.csv',names= ['date','x','y'], sep=' ')

now data is a data frame containing the entries in the file data.csv. however all columns are have str() type. You have to convert the column (to parse it) to datetime using the pandas to_datetime() function:

data['date'] = pd.to_datetime(data['date'])

now you can continue with the rest of the code:

date = data['date']
date_difference = date[3]- date[2]

print(date_difference)

that should give the following result:

Timedelta('4 days 00:00:00')

Note that date_difference is a Timedelta variable.

For more on dealing with dates check:
https://docs.python.org/3/library/datetime.html

For more on pandas.read_csv check: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

For more on pandas.to_datetime check:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html



Related Topics



Leave a reply



Submit