I have a forcast of 24 months in my Dataframe, how can I filter the date to 12 months
I know how to filter by a fixed date.
But my dates are always extended by one month. So I need a variable filter.
My solution should be to filter 12 months from the current month on.
Thanks a lot
Try this:
from datetime import date
from dateutil.relativedelta import relativedelta
df = df[df['Date_column_name'] >= (date.today() + relativedelta(months=+12))]
Hope it helps...
Related
I try to keep only month and year in this df. I tried several solutions but it is not working. Can you help me ?
YOu need to do this (as you post no data, you'll need to adapt this to your case):
from datetime import datetime
datetime_object = datetime.now()
print(datetime_object)
2021-11-30 15:57:20.812209
And to get the year and month do this:
new_date_month= datetime_object.month
print(new_date_month)
new_date_year = datetime_object.year
print(new_date_year)
11
2021
If you need them as new columns in you df:
df['year']=datetime_object.year
df['Month']=datetime_object.month
Note that if your column is not a datetime, this will not work. Given to format of date you hve you will need to do this first:
st = '2021-11-30 15:57:20.812209'
datetime.strptime(st, '%Y-%m-%d %H:%M:%S.%f')
Is there any way based on a date to take the last one month data? I have searched a lot but I can't find a good and precise solution. If the current date is on index 420 the date is 2012-01-09. I want to have a data frame with data from 2011-12-09 until 2012-01-09.
import pandas as pd
import numpy as np
times = pd.DataFrame(pd.date_range('2012-01-01', '2012-04-01', freq='30min'), columns=['date'])
times['date'] = pd.to_datetime(times['date'])
times['value'] = np.random.randint(1, 6, times.shape[0])
months = times.iloc[0:420].sort_values(by='date', ascending=True).set_index('date').last('1M')
Using the .last command the results end on 2012-01-01 as this is the last month. I understand it but is there any way to find the last one month data without using timedelta or relative delta? In the case of both if a date is missing then an error appears which is also a problem.
Thank you.
I think what you're looking for is pd.Period. You can convert all of your datetimes to a month period and then search using that
# turn your datetimes to month periods
times["month"] = times[times["date"].dt.to_period("m")]
# turn your search date to a period
your_date = pd.Period(your_date, "m")
# search times
times[times["month"] == your_date]
So i would like to make a column name call date
The first entry i would make it today's Date, i.e. 23/07/2019
and the following row to be the date + 1 i.e. 24/07/2019 so on...
This is easily done in Excel but i tried this simple thing in pandas and i just cant figure out how!
I already have a dateframe called df
so to put down todays date is relatively simple.
df.Date = pd.datetime.now().date()
But im not sure which function would get me the date+1 in the following rows.
Thanks
pd.date_range can use 'today' to set the dates. Normalize then create the Series yourself, otherwise pandas thinks the DatetimeIndex should be the Index too.
import pandas as pd
pd.Series(pd.date_range('today', periods=30, freq='D').normalize(),
name='Date')
0 2019-07-23
1 2019-07-24
...
28 2019-08-20
29 2019-08-21
Name: Date, dtype: datetime64[ns]
If adding a new column to the DataFrame:
df['Date'] = pd.date_range('today', periods=len(df), freq='D').normalize()
pd.date_range is what you are looking for. To build a series of 31 days starting from today:
today = pd.Timestamp.now().normalize()
s = pd.date_range(today, today + pd.Timedelta(days=30), freq='D').to_series()
I have in a dataframe:
Customer ID,
Customer date of birth,
date of purchase.
I need a function to calculate the distance in +/- days between the date of birth and the date of purchase
for example if date of birth is 20/12/1960 and date of purchase is 16/01/2019 I need to have 27 which is 27 days after the birthday or if the date of purchase is 05/12/2018 I need -15 which is 15 days before the birthday.
Any suggestions?
Since you need to stay within the year of purchase, you need to extract the day of the year for the birthday and the purchase date which can be done using .dt.dayofyear as follows:
import pandas as pd
import numpy as np
df=pd.DataFrame({'customer_id':[1,2,3],
'birthday':pd.to_datetime(['20/12/1960','2/6/1980','6/1/1972']),
'purchase_date':pd.to_datetime(['1/1/2004','5/25/2018','3/4/2010'])})
df['days_away']=df['birthday'].dt.dayofyear - df['purchase_date'].dt.dayofyear
df
You can separate this into two. First, create a new column for the looked up birthday date. Second, subtract these two date columns to get the timedelata (and do .dt.days to get this in days).
Why does the following code produce the same results?
Is there a way to subtract date from pandas TimeStamp?
print s['ADM.DT'] + pd.DateOffset(month=2)
print s['ADM.DT'] - pd.DateOffset(month=2)
s['ADM.DT'] is pandas.tslib.Timestamp object.
If you use
pd.DateOffset(month=2)
It shifts the Date to the second month of the year. If you want to shift the date for 2 months you have to use:
pd.DateOffset(months=2)