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