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:
CalendarDate BusinessDay 2 2022-05-04 1 3 2022-05-03 1 4 2022-05-02 1 5 2022-04-29 1 6 2022-04-28 1 7 2022-04-27 1 8 2022-04-26 1 CalendarDate datetime64[ns] BusinessDay int64 dtype: object
This is the function that turns a dataframe column into a list:
def slice_single_dataframe_column_into_list(dataframe, column_name): sliced_column = dataframe.loc[:,[column_name]].values.tolist() print(sliced_column) column_list = [] for element in sliced_column: column_list.append(element) return column_list
This is what is printed after the column is sliced:
[[1651622400000000000], [1651536000000000000], [1651449600000000000], [1651190400000000000], [1651104000000000000], [1651017600000000000], [1650931200000000000]]
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.
def slice_single_dataframe_column_into_list(dataframe, column_name): sliced_column = dataframe[column_name].tolist() # ^^ Changes here column_list = [] for element in sliced_column: column_list.append(element) return column_list