Create new column with day of week using datetime - python

I'm trying to create a new column in this DataFrame with the name of the day of the week, but I can't. Does anyone have any tips?
from datetime import date
days = [
'Segunda-feira',
'Terça-feira',
'Quarta-feira',
'Quinta-feira',
'Sexta-feira',
'Sábado',
'Domingo'
]
import pandas as pd
vacinacao = pd.read_excel("vacinacao_br.xlsx")
vacinacao.head()
UF Data Vacinacao quantidade
0 AC 2021-01-18 00:00:00 1
1 AC 2021-01-19 00:00:00 46
2 AC 2021-01-20 00:00:00 1021
3 AC 2021-01-21 00:00:00 1609
4 AC 2021-01-22 00:00:00 1105
vacinacao['dia_semana'] = vacinacao['Data Vacinacao'].days.weekday.()
AttributeError: 'Series' object has no attribute 'days'

Assuming the dtype of your "Data Vacinado" column is some sort of daytime, you can use dt.day_name.
vacinacao['dia_semana'] = vacinacao['Data Vacinacao'].dt.day_name()
NB: If the dtype of your column is a str, you'll have to convert your dates to datetime objects first:
vacinacao['Data Vacinacao'] = pd.to_datetime(vacinacao['Data Vacinacao'], format="%Y-%m-%d")

You want dt and not days
Try:
#for day number (0, 1, etc.)
vacinacao['dia_semana'] = vacinacao['Data Vacinacao'].dt.weekday
#for day full name (Monday, Tuesday, etc.)
vacinacao['dia_semana'] = vacinacao['Data Vacinacao'].dt.strftime("%A")
#for day short name (Mon, Tue, etc.)
vacinacao['dia_semana'] = vacinacao['Data Vacinacao'].dt.strftime("%a")

Look up from your days list with .weekday() integer:
vacinacao['Data Vacinacao'] = pd.to_datetime(vacinacao['Data Vacinacao'],
format="AC %Y-%m-%d %H:%M:%S")
vacinacao['dia_semana'] = [days[i.weekday()] for i in vacinacao['Data Vacinacao']]

Related

Filtering Pandas Dataframe on Time (not Date)

I have the dataframe below and want to filter by time. The time column comes up as an object when I use dtypes.
To get the time to use as the filter criteria I use split:
start_time = "25 September 2022, 13:00:00"
split_time = start_time.split(", ")[1]
I have tried converting split_time and the df column to datime but get an error on the df column conversion:
TypeError: <class 'datetime.time'> is not convertible to datetime
I have also tried a simple string search but this doesn't return any results.
I have been able to filter by date using:
split_date = start_time.split(", ")[0]
event_date = datetime.strptime(split_date, "%d %B %Y")
events_df['start_date'] = pd.to_datetime(events_df['start_date'])
filtered_df = events_df.loc[(events_df['start_date'] == event_date)]
But can't seem to do the equivalent for time. Can anyone see the problem?
Thanks
fixture_id
name
start_date
time
145
9394134
Plymouth Argyle v Ipswich Town
2022-09-25 00:00:00
12:30:00
146
9694948
Grays Athletic v Merstham FC
2022-09-25 00:00:00
13:00:00
147
9694959
FC Romania v Faversham Town
2022-09-25 00:00:00
15:00:00
Comapre times generated by Series.dt.time with Timestamp.time:
start_time = "25 September 2022, 13:00:00"
dt = pd.to_datetime(start_time, format="%d %B %Y, %H:%M:%S")
events_df['start_date'] = pd.to_datetime(events_df['start_date'])
#if necessary
events_df['time'] = pd.to_datetime(events_df['time']).dt.time
filtered_df = events_df.loc[(events_df['time'] == dt.time())]
print (filtered_df)
fixture_id name start_date time
1 146 9694948 Grays Athletic v Merstham FC 2022-09-25 13:00:00

Parsing dates in pandas.to_datetime when date is 'DD-MMM' [duplicate]

I have a column in the following format
Date
June 22
June 23
June 24
June 25
I am trying to convert this column to datetime within a pandas df with the format YYYY-mm-dd
How can I accomplish this? I was able to format the date and convert to mm-dd but not sure how to add the current's year since it's not present in my Date column
df['Date'] = pd.to_datetime(df['Date'], format='%B %d')
Results:
Date
1900-07-22
1900-07-21
1900-07-20
1900-07-19
Desired results:
Date
2021-07-22
2021-07-21
2021-07-20
2021-07-19
Try:
>>> pd.to_datetime(df['Date'].add(' 2021'), format="%B %d %Y")
0 2021-06-22
1 2021-06-23
2 2021-06-24
3 2021-06-25
Name: Date, dtype: datetime64[ns]
Suggested by #HenryEcker, to add the current year instead of specifying 2021:
pd.to_datetime(df['Date'].add(f' {pd.Timestamp.now().year}'), format="%B %d %Y")

Parse Month Day ('%B %d') date column into datetime using current year

I have a column in the following format
Date
June 22
June 23
June 24
June 25
I am trying to convert this column to datetime within a pandas df with the format YYYY-mm-dd
How can I accomplish this? I was able to format the date and convert to mm-dd but not sure how to add the current's year since it's not present in my Date column
df['Date'] = pd.to_datetime(df['Date'], format='%B %d')
Results:
Date
1900-07-22
1900-07-21
1900-07-20
1900-07-19
Desired results:
Date
2021-07-22
2021-07-21
2021-07-20
2021-07-19
Try:
>>> pd.to_datetime(df['Date'].add(' 2021'), format="%B %d %Y")
0 2021-06-22
1 2021-06-23
2 2021-06-24
3 2021-06-25
Name: Date, dtype: datetime64[ns]
Suggested by #HenryEcker, to add the current year instead of specifying 2021:
pd.to_datetime(df['Date'].add(f' {pd.Timestamp.now().year}'), format="%B %d %Y")

unsupported operand type(s) for +: 'Timestamp' and 'Timestamp' [duplicate]

I just want to extract from my df HH:MM. How do I do it?
Here's a description of the column in the df:
count 810
unique 691
top 2018-07-25 11:14:00
freq 5
Name: datetime, dtype: object
The string value includes a full time stamp. The goal is to parse each row's HH:MM into another df, and to loop back over and extract just the %Y-%m-%d into another df.
Assume the df looks like
print(df)
date_col
0 2018-07-25 11:14:00
1 2018-08-26 11:15:00
2 2018-07-29 11:17:00
#convert from string to datetime
df['date_col'] = pd.to_datetime(df['date_col'])
#to get date only
print(df['date_col'].dt.date)
0 2018-07-25
1 2018-08-26
2 2018-07-29
#to get time:
print(df['date_col'].dt.time)
0 11:14:00
1 11:15:00
2 11:17:00
#to get hour and minute
print(df['date_col'].dt.strftime('%H:%M'))
0 11:14
1 11:15
2 11:17
First convert to datetime:
df['datetime'] = pd.to_datetime(df['datetime'])
Then you can do:
df2['datetime'] = df['datetime'].dt.strptime('%H:%M')
df3['datetime'] = df['datetime'].dt.strptime('%Y-%m-%d')
General solution (not pandas based)
import time
top = '2018-07-25 11:14:00'
time_struct = time.strptime(top, '%Y-%m-%d %H:%M:%S')
short_top = time.strftime('%H:%M', time_struct)
print(short_top)
Output
11:14

Pandas - How to extract HH:MM from datetime column in Python?

I just want to extract from my df HH:MM. How do I do it?
Here's a description of the column in the df:
count 810
unique 691
top 2018-07-25 11:14:00
freq 5
Name: datetime, dtype: object
The string value includes a full time stamp. The goal is to parse each row's HH:MM into another df, and to loop back over and extract just the %Y-%m-%d into another df.
Assume the df looks like
print(df)
date_col
0 2018-07-25 11:14:00
1 2018-08-26 11:15:00
2 2018-07-29 11:17:00
#convert from string to datetime
df['date_col'] = pd.to_datetime(df['date_col'])
#to get date only
print(df['date_col'].dt.date)
0 2018-07-25
1 2018-08-26
2 2018-07-29
#to get time:
print(df['date_col'].dt.time)
0 11:14:00
1 11:15:00
2 11:17:00
#to get hour and minute
print(df['date_col'].dt.strftime('%H:%M'))
0 11:14
1 11:15
2 11:17
First convert to datetime:
df['datetime'] = pd.to_datetime(df['datetime'])
Then you can do:
df2['datetime'] = df['datetime'].dt.strptime('%H:%M')
df3['datetime'] = df['datetime'].dt.strptime('%Y-%m-%d')
General solution (not pandas based)
import time
top = '2018-07-25 11:14:00'
time_struct = time.strptime(top, '%Y-%m-%d %H:%M:%S')
short_top = time.strftime('%H:%M', time_struct)
print(short_top)
Output
11:14

Categories

Resources