我正在尝试为比一个星期前更长的日期上色。 但是当我这样做时,它会为所有日期着色。
首先我在破折号之前尝试过它,并且效果很好
df = pd.DataFrame(list(collection_jobs.find({"1_Date":{'$regex':Date},"2_Color":"red"}, {'_id': False})))
df.columns = ['Date', 'Color', 'Name', 'Description', 'Last Build', 'Last Build Result', 'Last Failed Build', 'Last Successful Build', 'Last Unsuccessful Build']
print(df['Last Successful Build'])
>>>0 05/07/2019 09:48:29
1 15/07/2019 08:35:59
2 12/06/2019 08:21:46
3 06/07/2019 01:25:00
4 13/07/2019 21:55:00
5 13/07/2019 21:30:00
6 11/07/2019 03:49:00
7 13/07/2019 20:22:00
8 15/06/2019 11:42:00
9 11/07/2019 01:37:00
Name: Last Successful Build, dtype: object
df['Last Successful Build'] = pd.to_datetime(df['Last Successful Build'], format="%d/%m/%Y %H:%M:%S")
week_ago = datetime.today() - timedelta(days=7)
print("")
print(week_ago)
>>>2019-07-10 11:48:40.377170
print("")
print(df['Last Successful Build'])
>>> 0 2019-07-05 09:48:29
1 2019-07-15 08:35:59
2 2019-06-12 08:21:46
3 2019-07-06 01:25:00
4 2019-07-13 21:55:00
5 2019-07-13 21:30:00
6 2019-07-11 03:49:00
7 2019-07-13 20:22:00
8 2019-06-15 11:42:00
9 2019-07-11 01:37:00
Name: Last Successful Build, dtype: datetime64[ns]
print("")
result = df['Last Successful Build'] < week_ago
print(result)
>>>0 True
1 False
2 True
3 True
4 False
5 False
6 False
7 False
8 True
9 False
Name: Last Successful Build, dtype: bool
这是我对代码进行过滤/着色的地方
style_data_conditional=[
{
'if': {
'column_id' : 'Last Successful Build',
'filter_query': '{Last Successful Build} < week_ago'
},
'backgroundColor': 'white',
'color': '#ed0909',
},
],
我希望输出使结果正确的行变色。
答案 0 :(得分:0)
我设法通过使数据像这样“ [Last Success Build”(上次成功构建)”中的>>> 2019-07-11 然后对于week_ago,我这样做:
week_ago = datetime.today() - timedelta(days=7)
week_agoDate = week_ago.strftime("%Y-%m-%d")
最后,在您创建数据表的破折号中,我将其更改为:
style_data_conditional=[
{
'if': {
'column_id' : 'Last Successful Build',
'filter_query': ('{Last Successful Build} <' + week_agoDate)
},
'backgroundColor': 'white',
'color': '#ed0909',
},
],