【问题标题】:Highlighting Date cells which are older than a week !!!Dash突出显示超过一周的日期单元格!!!Dash
【发布时间】:2019-07-17 09:55:52
【问题描述】:

我正在尝试为比一周前更长的日期着色。 但是当我这样做时,它会为所有日期着色。

首先我在我的破折号代码之前尝试过它,它运行良好

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',
            },                             
        ],

我希望输出为结果为真的线条着色。

【问题讨论】:

    标签: python pandas plotly-dash hyphen


    【解决方案1】:

    我已经设法通过像这样制作 ['Last Successful 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',
                },                             
            ], 
    

    【讨论】:

      【解决方案2】:

      我已经有正确的数据类型,如果我比较它们并打印它们,正确的输出为真,但由于某种原因,它会全部着色。 有人可以帮忙吗?

      dft['Datum'] = pd.to_datetime(dft["Datum"]).dt.date
      dft['erledigt am'] = pd.to_datetime(dft["erledigt am"]).dt.date
      
      week_ago = datetime.date.today() - timedelta(days=7)
      print(week_ago)
      print(dft['Datum'])
      
      res = dft['Datum'] < week_ago
      
      print(res)
      

      这是正确评估较早日期(打印资源)。然而在这里:

      style_data_conditional= ( 
      
      { 
      'if': 
        { 'filter_query': '{Datum} < week_ago ',
          'column_id ': 'Datum '
        },
        'backgroundColor ': 'lightcoral ',
      },
      
                                )
      

      它为该列中带有日期的每个单元格着色。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-15
        • 2013-07-09
        • 2015-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多