Reputation: 615
I would like to extract a week number from a data in panda dataframe but starting from the SPECIFIC date.
For example from 4th April:
20/04/2010 --> 1
27/04/2010 --> 2
04/05/2010 --> 3
and so on..
Any idea?
Thank you in advance!
Upvotes: 2
Views: 1107
Reputation: 18188
Use pandas to_datetime to parse your date column if it is not already in datetime format.
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html
Then use the date time method weekday.
https://docs.python.org/2/library/datetime.html
my_dataframe['week_nums'] = pandas.to_datetime(my_dataframe['date_col']).weekday()
Sorry I saw you want week day from specific date, will update answer... it is easy to calculate the difference between 2 dates.
Upvotes: 1
Reputation: 54223
Just calculate the difference in days between 2 dates, divide by 7 and add 1 :
from datetime import date
origin = date(2010, 4, 20)
def week_number_from(my_date, origin):
return (my_date - origin).days / 7 + 1
Upvotes: 2