Extracting just Month and Year separately from Pandas Datetime column
If you want new columns showing year and month separately you can do this:
df['year'] = pd.DatetimeIndex(df['ArrivalDate']).yeardf['month'] = pd.DatetimeIndex(df['ArrivalDate']).month
df['year'] = df['ArrivalDate'].dt.yeardf['month'] = df['ArrivalDate'].dt.month
Then you can combine them or work with them just as they are.
Best way found!!
df['date_column'] has to be in date time format.
df['month_year'] = df['date_column'].dt.to_period('M')
You could also use
D for Day,
2M for 2 Months etc. for different sampling intervals, and in case one has time series data with time stamp, we can go for granular sampling intervals such as
45Min for 45 min,
15Min for 15 min sampling etc.
You can directly access the
month attributes, or request a
In : t = pandas.tslib.Timestamp.now()In : tOut: Timestamp('2014-08-05 14:49:39.643701', tz=None)In : t.to_pydatetime() #datetime method is deprecatedOut: datetime.datetime(2014, 8, 5, 14, 49, 39, 643701)In : t.dayOut: 5In : t.monthOut: 8In : t.yearOut: 2014
One way to combine year and month is to make an integer encoding them, such as:
201408 for August, 2014. Along a whole column, you could do this as:
df['YearMonth'] = df['ArrivalDate'].map(lambda x: 100*x.year + x.month)
or many variants thereof.
I'm not a big fan of doing this, though, since it makes date alignment and arithmetic painful later and especially painful for others who come upon your code or data without this same convention. A better way is to choose a day-of-month convention, such as final non-US-holiday weekday, or first day, etc., and leave the data in a date/time format with the chosen date convention.
calendar module is useful for obtaining the number value of certain days such as the final weekday. Then you could do something like:
import calendarimport datetimedf['AdjustedDateToEndOfMonth'] = df['ArrivalDate'].map( lambda x: datetime.datetime( x.year, x.month, max(calendar.monthcalendar(x.year, x.month)[-1][:5]) ))
If you happen to be looking for a way to solve the simpler problem of just formatting the datetime column into some stringified representation, for that you can just make use of the
strftime function from the
datetime.datetime class, like this:
In : dfOut: date_time0 2014-10-17 22:00:03In : df.date_timeOut: 0 2014-10-17 22:00:03Name: date_time, dtype: datetime64[ns]In : df.date_time.map(lambda x: x.strftime('%Y-%m-%d'))Out: 0 2014-10-17Name: date_time, dtype: object