I want query the issue's count of group by weekly.
query1 = MyModel.object.filter(issue_creator__in=group.user_set.all()).\
annotate(week=TruncWeek('issue_creat_date')).values('week').annotate(count=Count('id')).order_by('week'))
the query result is OK. the queryset result:
[
{'week': datetime.datetime(2022, 1, 3, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 9},
{'week': datetime.datetime(2022, 1, 10, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 12},
{'week': datetime.datetime(2022, 1, 17, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 10},
{'week': datetime.datetime(2022, 2, 7, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 1},
{'week': datetime.datetime(2022, 2, 14, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 6},
{'week': datetime.datetime(2022, 2, 21, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 11},
{'week': datetime.datetime(2022, 2, 28, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 1}
]
but 20220101-20220301 has 9 weeks:
[
datetime.datetime(2022, 1, 3, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>),
datetime.datetime(2022, 1, 10, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>),
datetime.datetime(2022, 1, 17, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>),
datetime.datetime(2022, 1, 24, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>),
datetime.datetime(2022, 1, 31, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>),
datetime.datetime(2022, 2, 7, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>),
datetime.datetime(2022, 2, 14, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>),
datetime.datetime(2022, 2, 21, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>),
datetime.datetime(2022, 2, 28, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>)
]
I want add zero when that week no result as this result:
[
{'week': datetime.datetime(2022, 1, 3, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 9},
{'week': datetime.datetime(2022, 1, 10, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 12},
{'week': datetime.datetime(2022, 1, 17, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 10},
{'week': datetime.datetime(2022, 1, 24, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 0},
{'week': datetime.datetime(2022, 1, 31, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 0},
{'week': datetime.datetime(2022, 2, 7, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 1},
{'week': datetime.datetime(2022, 2, 14, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 6},
{'week': datetime.datetime(2022, 2, 21, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 11},
{'week': datetime.datetime(2022, 2, 28, 0, 0, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>), 'count': 1}
]
how to write the right queryset?
Thanks.
Django method for None value Coalesce.
from django.db.models.functions import Coalesce
query1 = MyModel.object.filter(issue_creator__in=group.user_set.all()).\
annotate(week=TruncWeek('issue_creat_date')).values('week').annotate(count=Count('id')).order_by('week'))
I have a spreadsheet imported with pandas like this:
df = pd.read_excel('my_spreadsheet.xlsx',header = [0,1],index_col=0,sheetname='Sheet1')
The output of df.columns is:
MultiIndex(levels=[[u'MR 1', u'MR 10', u'MR 11', u'MR 12', u'MR 13', u'MR 14', u'MR 15', u'MR 16', u'MR 17', u'MR 18', u'MR 19', u'MR 2', u'MR 20', u'MR 21', u'MR 22', u'MR 3', u'MR 4', u'MR 5', u'MR 6', u'MR 7', u'MR 8', u'MR 9'], [u'BIRADS', u'ExamDesc', u'completedDTTM']],
labels=[[0, 0, 0, 11, 11, 11, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 12, 12, 12, 13, 13, 13, 14, 14, 14], [1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0]],
names=[None, u'De-Identified MRN'])
I have been trying to access the values of column named 'De-Identified MRN', but can't seem to find the way to do this.
What I have tried (based on similar posts):
[in] df.index.get_level_values('De-Identified MRN')
[out] KeyError: 'Level De-Identified MRN must be same as name (None)'
and
[in] df.index.unique(level='De-Identified MRN')
[out] KeyError: 'Level De-Identified MRN must be same as name (None)'
UPDATE:
The following did the trick for some reason. I really do not understand the format of the MultiIndex Pandas Dataframe:
pd.Series(df.index)
By using your data
s="MultiIndex(levels=[[u'MR 1', u'MR 10', u'MR 11', u'MR 12', u'MR 13', u'MR 14', u'MR 15', u'MR 16', u'MR 17', u'MR 18', u'MR 19', u'MR 2', u'MR 20', u'MR 21', u'MR 22', u'MR 3', u'MR 4', u'MR 5', u'MR 6', u'MR 7', u'MR 8', u'MR 9'], [u'BIRADS', u'ExamDesc', u'completedDTTM']],labels=[[0, 0, 0, 11, 11, 11, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 12, 12, 12, 13, 13, 13, 14, 14, 14], [1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0]],names=[None, u'De-Identified MRN'])"
idx=eval(s, {}, {'MultiIndex': pd.MultiIndex})
df=pd.DataFrame(index=idx)
df.index.get_level_values(level=1) # df.index.get_level_values('De-Identified MRN')
Out[336]:
Index(['ExamDesc', 'completedDTTM', 'BIRADS', 'ExamDesc', 'completedDTTM',
'BIRADS', 'ExamDesc', 'completedDTTM', 'BIRADS', 'ExamDesc',...
Also if all above still does not work , try
df.reset_index()['De-Identified MRN']
Try the following:
midx = pd.MultiIndex(
levels=[[u'MR 1', u'MR 10', u'MR 11', u'MR 12', u'MR 13', u'MR 14', u'MR 15', u'MR 16', u'MR 17', u'MR 18', u'MR 19', u'MR 2', u'MR 20', u'MR 21', u'MR 22', u'MR 3', u'MR 4', u'MR 5', u'MR 6', u'MR 7', u'MR 8', u'MR 9'], [u'BIRADS', u'ExamDesc', u'completedDTTM']],
labels=[[0, 0, 0, 11, 11, 11, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 12, 12, 12, 13, 13, 13, 14, 14, 14], [1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0]],
names=[None, u'De-Identified MRN']
)
midx.levels[1] # returns the following
Index(['BIRADS', 'ExamDesc', 'completedDTTM'], dtype='object', name='De-Identified MRN')
midx.levels[1].values # returns the following
array(['BIRADS', 'ExamDesc', 'completedDTTM'], dtype=object)
Is it the best way to group and merge each reduction into a single document with sub-array?
r.expr([
{id: 1, foo: 1, bar: 2, date: r.time(2016, 1, 1, 'Z')},
{id: 1, foo: 4, bar: 1, date: r.time(2016, 1, 3, 'Z')},
{id: 1, foo: 10, bar: 0, date: r.time(2016, 1, 2, 'Z')},
{id: 2, foo: 5, bar: 3, date: r.time(2016, 1, 1, 'Z')},
{id: 2, foo: 3, bar: 6, date: r.time(2016, 1, 2, 'Z')}
]).group('id').orderBy('date').map(function(d){
return d
.without('foo', 'bar', 'date')
.merge({stats: [d.pluck('foo', 'bar', 'date')]})
}).reduce(function(left, right){
return left .without('stats').merge({
stats: left('stats').append(right('stats')(0))
})
}).ungroup().map(function(g){
return g('reduction')
})
Output:
[
{
"id": 1 ,
"stats": [
{
"foo": 1, "bar": 2 , "date": Fri Jan 01 2016 00:00:00 GMT+00:00
},
{
"foo": 10, "bar": 0 , "date": Sat Jan 02 2016 00:00:00 GMT+00:00
} ,
{
"foo": 4, "bar": 1 , "date": Sun Jan 03 2016 00:00:00 GMT+00:00
}
]
},
{
"id": 2 ,
"stats": [
{
"foo": 5, "bar": 3, "date": Fri Jan 01 2016 00:00:00 GMT+00:00
} ,
{
"foo": 3, "bar": 6, "date": Sat Jan 02 2016 00:00:00 GMT+00:00
}
]
}
]
This should work:
r.expr([
{id: 1, foo: 1, bar: 2, date: r.time(2016, 1, 1, 'Z')},
{id: 1, foo: 4, bar: 1, date: r.time(2016, 1, 3, 'Z')},
{id: 1, foo: 10, bar: 0, date: r.time(2016, 1, 2, 'Z')},
{id: 2, foo: 5, bar: 3, date: r.time(2016, 1, 1, 'Z')},
{id: 2, foo: 3, bar: 6, date: r.time(2016, 1, 2, 'Z')}
]).group('id')
.orderBy('date')
.without('id')
.ungroup()
.map(rec => { return { id : rec('group'), stats : rec('reduction') }; } )
I have a stacked bar chart with annotations which sums the values. The annotations are always at the end of the bar, but when there isn't a value for the last data row (I) the annotation is at the beginning and I don't know how to fix it.
var dataArray = [
["Date", "A", "B", "C", "D", "E", "F", "G", "H", "I", {role: 'annotation'}],
["7.08.2015", 0, 0, 0, 3, 6, 1, 0, 0, 0, 10],
["6.08.2015", 0, 0, 0, 0, 4, 6, 1, 0, 7, 18],
["5.08.2015", 0, 0, 0, 2, 4, 0, 0, 0, 5, 11]
];
Demo and code at JSFiddle
Found a workaround ... added a new data column, with the name Total, is has the same value as the annotation:
var dataArray = [
["Date", "A", "B", "C", "D", "E", "F", "G", "H", "I", "Total", {role: 'annotation'}],
["7.08.2015", 0, 0, 0, 3, 6, 1, 0, 0, 0, 10, 10],
["6.08.2015", 0, 0, 0, 0, 4, 6, 1, 0, 7, 18, 18],
["5.08.2015", 0, 0, 0, 2, 4, 0, 0, 0, 5, 11, 11]
];
And added this to the options:
var options = {
...
series: {
9: {
color: 'transparent',
type: "bar",
targetAxisIndex: 1,
visibleInLegend: false
}
}
};
Demo and code at JSFiddle
This makes the Total bar transparent, hide it in the legends and let it start from the zero point.
Dynamic version which takes the last data row for the annotations:
var series = {};
series[data.getNumberOfColumns() - 3] = {
color: 'transparent',
type: "bar",
targetAxisIndex: 1,
visibleInLegend: false
};
options["series"] = series;
Demo and code at JSFiddle