Skip to content
Advertisement

Problem: Pandas – Slicing a datetime64[ns] column returns a list of 19-digit integers

I am trying to convert a column of dates in a dataframe to a list, but something is happening when I slice it that turns the dates into 19-digit integers. This is the original df along with the column dtypes:

JavaScript

This is the function that turns a dataframe column into a list:

JavaScript

This is what is printed after the column is sliced:

JavaScript

I have tried converting that int as if it was a timestamp, but I get ‘invalid arguement’. I’m just not sure what is happening here. I would like the items in the list to look exactly as they do in the dataframe.

Advertisement

Answer

pd.to_datetime('1970-01-01').value returns the UNIX timestamp and df[datetimecolumn].values.tolist() seems returns the UNIX timestamp directly.

You can avoid by calling tolist on Series directly.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement