'Python Pandas Error: 'DataFrame' object has no attribute 'Datetime' when trying to create an average of time periods

I am trying to create a dataframe (df) that creates a sample sums, means, and standard deviations of the following 12 monthly return series by month from another dataframe cdv file called QUESTION1DATA.csv and he head of performance data looks like this: enter image description here.

So far I have created a code to find what I am looking for and have come up with this:

import time
df['Timestamp'] = portlist.to_datetime(df['Year'],format='%Y') 

new_df = df.groupby([df.index.year, df.index.month]).agg(['sum', 'mean', 'std'])
new_df.index.set_names(['Year', 'Month'], inplace = True)
new_df.reset_index(inplace = True)

However when I run this code I get this error and don't know where to go from there.

`"list' object has no attribute 'to_datetime'

I



Solution 1:[1]

to_datetime is available in Pandas and should be use as follows:

>>pd.to_datetime('13000101', format='%Y%m%d', errors='ignore')
datetime.datetime(1300, 1, 1, 0, 0)

In your case portlist is a list and hence the code is throwing error.

In your case it should be as follows:

df['Timestamp'] = pd.to_datetime(df['Year'],format='%Y') 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vaibhav Jadhav