python - Time Delta of Year Excluding Certain Days -


i making heat map has company name on x axis, months on y-axis, , shaded regions number of calls.

i taking slice of data database past year in order create heat map. however, means if hover on current month, example today july 13, calls of july 1-13 of year, , calls of july 13-31 last year added together. in current month, want show calls july 1-13.

#this section selects last year of data # convert strings datetimes df['recvd_dttm'] = pd.to_datetime(df['recvd_dttm'])  #only retrieve data before (ignore typos future dates) mask = df['recvd_dttm'] <= datetime.datetime.now() df = df.loc[mask] # first , last datetime final week of data  range_max = df['recvd_dttm'].max() range_min = range_max - datetime.timedelta(days=365)  # take slice final week of data df = df[(df['recvd_dttm'] >= range_min) &                 (df['recvd_dttm'] <= range_max)] 

you can use pd.tseries.offsets.monthend() achieve goal here.

import pandas pd import numpy np import datetime dt  np.random.seed(0) val = np.random.randn(600) date_rng = pd.date_range('2014-01-01', periods=600, freq='d')  df = pd.dataframe(dict(dates=date_rng,col=val)) print(df)          col      dates 0    1.7641 2014-01-01 1    0.4002 2014-01-02 2    0.9787 2014-01-03 3    2.2409 2014-01-04 4    1.8676 2014-01-05 5   -0.9773 2014-01-06 6    0.9501 2014-01-07 7   -0.1514 2014-01-08 8   -0.1032 2014-01-09 9    0.4106 2014-01-10 ..      ...        ... 590  0.5433 2015-08-14 591  0.4390 2015-08-15 592 -0.2195 2015-08-16 593 -1.0840 2015-08-17 594  0.3518 2015-08-18 595  0.3792 2015-08-19 596 -0.4700 2015-08-20 597 -0.2167 2015-08-21 598 -0.9302 2015-08-22 599 -0.1786 2015-08-23  [600 rows x 2 columns]   print(df.dates.dtype) datetime64[ns]   datetime_now = dt.datetime.now() datetime_now_month_end = datetime_now + pd.tseries.offsets.monthend(1)  print(datetime_now_month_end) 2015-07-31 03:19:18.292739   datetime_start = datetime_now_month_end - pd.tseries.offsets.dateoffset(years=1)  print(datetime_start) 2014-07-31 03:19:18.292739   print(df[(df.dates > datetime_start) & (df.dates < datetime_now)])          col      dates 212  0.7863 2014-08-01 213 -0.4664 2014-08-02 214 -0.9444 2014-08-03 215 -0.4100 2014-08-04 216 -0.0170 2014-08-05 217  0.3792 2014-08-06 218  2.2593 2014-08-07 219 -0.0423 2014-08-08 220 -0.9559 2014-08-09 221 -0.3460 2014-08-10 ..      ...        ... 550  0.1639 2015-07-05 551  0.0963 2015-07-06 552  0.9425 2015-07-07 553 -0.2676 2015-07-08 554 -0.6780 2015-07-09 555  1.2978 2015-07-10 556 -2.3642 2015-07-11 557  0.0203 2015-07-12 558 -1.3479 2015-07-13 559 -0.7616 2015-07-14  [348 rows x 2 columns] 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -