0

I have two dataframes as two different ColumnDataSources for updating plots. One looks like

ENTRYDATE | Transactions
2017-01-01  290
2017-01-03  20
..
2018-02-04  118

The other dataframe's first column is a float of the first dataframe consisting of the year, followed by the week number of that year, separated by a colon

ENTRYDATE | Transactions
2017.1      310
2018.1      118

I have a Select Widget that updates a line plot based on whether the user wants to view daily data or weekly data. The daily plot renders fine. However, when weekly is selected, the plot treats the weeks as number data there are large gaps from one data point to the next (i.e. 2017.52 to 2018.1)

How can I edit my code so that when the user makes a selection, the weekly data is presented categorically? Is there a better way to represent weeks across several calendar years?

Here is the relevant piece of my code:

dfdate=(df2.groupby([df2['ENTRYDATE'].dt.date]).size().reset_index(name='Transactions'))
#WEEK
df['YearWk']=df['ENTRYDATE'].dt.strftime('%Y.%W') #add year column
dfweek=(df.groupby([df['YearWk']]).size().reset_index(name='Transactions'))
dfweek.columns.values[0]='ENTRYDATE' #rename column to ENTRYDATE 
dfweek.sort_values('ENTRYDATE',ascending=True) #sort by Year.WeekNumber

sourceDay=ColumnDataSource(data=dfdate)
sourceWeek=ColumnDataSource(data=dfweek)

p=figure(plot_width=800,plot_height=500, y_axis_label="Transaction Count",
x_axis_label="Date",background_fill_color='beige')
p.line(x="ENTRYDATE",y="Transactions",color='blue', source=sourceDay)
p.xaxis.formatter=DatetimeTickFormatter()

#update function
def update_plot(attr, old, new):
    if new=='Daily': 
        sourceDay.data={"ENTRYDATE":dfdate["ENTRYDATE"],"Transactions":dfdate["Transactions"]}
    elif new=='Weekly':
        sourceDay.data=sourceWeek.data
        p.x_range=str(dfweek["ENTRYDATE"].tolist())

#selecttool
select=Select(title='Choose Your Time Interval:', options=['Daily','Weekly'], value='daily')
select.on_change('value',update_plot)
layout=row(select, p)
curdoc().add_root(layout)

I've tried setting an x_range via p.x_range=dfweek["ENTRYDATE"].tolist() and p.x_range.set=dfweek["ENTRYDATE"].tolist() but no avail.

machump
  • 1,207
  • 2
  • 20
  • 41

1 Answers1

0

Convert the week and year to datetime via df = pd.to_datetime(df.YearWk.add('-0'), format='%Y-%W-%w') as per Parsing week of year to datetime objects with pandas

machump
  • 1,207
  • 2
  • 20
  • 41