I wrote a script for receiving data from Twitter, now I'm doing column splitting, I want the date and time to be in separate columns.
I get a date like: Wed Oct 20 16:42:04 0000 2021
I do it with the following code:
filtered_data['date'] = tweet['created_at']
Next, I want to convert this date at the time of receipt into two fields using datetime
date_formats = '%d-%m-%Y'
time_formats = '%H:%M:%S'
At the time of application:
filtered_data['date'] = datetime.strptime(tweet['created_at'], date_formats)
I get the following error:
time data 'Wed Oct 20 16:42:04 0000 2021' does not match format '%d-%m-%Y'
Tell me how I can do this transformation and is it possible to do it at all
CodePudding user response:
You need to use the current format first. See an example:
from datetime import datetime
my_date = 'Wed Oct 20 16:42:04 0000 2021'
initial_format = '%a %b %d %H:%M:%S %z %Y'
final_format = '%d-%m-%Y'
new_date = datetime.strptime(my_date, initial_format).strftime(final_format)
print(new_date)
Output:
20-10-2021
So in your case, try:
filtered_data['date'] = datetime.strptime(tweet['created_at'], '%a %b %d %H:%M:%S %z %Y').strftime(date_formats)
CodePudding user response:
If you want a datetime object:
given
tweet['created_at'] = 'Wed Oct 20 16:42:04 0000 2021'
use
input_format = '%a %b %d %H:%M:%S %z %Y'
filtered_data['date'] = datetime.strptime( \
tweet['created_at'], \
input_format)
results in
>>> filtered_data['date']
datetime(2021, 10, 20, 16, 42, 4, tzinfo=datetime.timezone.utc)
If you want a formatted string as '%d-%m-%Y'
:
given
tweet['created_at'] = 'Wed Oct 20 16:42:04 0000 2021'
use
input_format = '%a %b %d %H:%M:%S %z %Y'
output_format = '%d-%m-%Y'
filtered_data['date'] = datetime.strptime( \
tweet['created_at'], \
input_format).strftime(output_format)
results in
>>> filtered_data['date']
'20-10-2021'