Issue
I have a two million timestamp data. I am trying to find first and last date that to in the YYYY-MM-DD
format so I can use them in saving file name. But, I found out that np.unique(df.index)
is fast (10s) and produces dates in the
datetime.date(2022, 6, 7)
format but df.index.strftime('%Y-%m-%d').unique()
gives the output I want but it takes more than 5 min, which is bad. So, I decided to use the former approach.
So, How do I convert something like datetime.date(2022, 6, 7)
to'2022-06-07'
?
Solution
Just put that into the str(...)
function:
import datetime
my_date = datetime.date(2022, 6, 7)
print(str(my_date)) # prints 2022-06-07
Technically, you can just print
it and not make it a string first. But putting it in str
means that instead of printing it, you could save that string to a variable.
If you need more advanced formatting options, then you can do what @FObersteiner suggested. But the format you want happens to be the default, so this will do if you just want that one format
Answered By - cocomac
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.