Reputation: 139
I got a dataframe like
rectime
0 2018-10-13 00:15:00
1 2018-10-13 00:30:00
2 2018-10-13 00:45:00
3 2018-10-13 01:00:00
The format of 'rectime' is datetime
.I'd like to translate it to a list by using
pandas.DataFrame.values
What it returns is something like this
[['2018-10-13T00:15:00.000000000']
['2018-10-13T00:30:00.000000000']
['2018-10-13T00:45:00.000000000']
['2018-10-13T01:00:00.000000000']]
Someone can tell me why there is a difference? And how can I get the list with the same context in 'rectime'?
Upvotes: 0
Views: 38
Reputation: 21274
Use dt.strftime()
:
df.rectime.dt.strftime("%Y-%m-%d %H:%M:%S").values
array(['2018-10-13 00:15:00', '2018-10-13 00:30:00', '2018-10-13 00:45:00',
'2018-10-13 01:00:00'], dtype=object)
Explanation: Pandas stores the datetime objects internally in ISO-8601 format, but it renders date strings in a more readable format when you're interacting with printed output.
When you convert out of Pandas into a list with .values
, you lose the pretty-print feature, and you get the full 8601 string.
You can use the dt.strftime
method to determine exactly how you want the string representation to look, then move it into a list using .values
.
Data:
df = pd.DataFrame({'rectime': {
0: pd.Timestamp('2018-10-13 00:15:00'),
1: pd.Timestamp('2018-10-13 00:30:00'),
2: pd.Timestamp('2018-10-13 00:45:00'),
3: pd.Timestamp('2018-10-13 01:00:00')}})
Upvotes: 2