昨天之前如何查询熊猫

时间:2018-07-16 12:20:45

标签: python pandas datetime dataframe timestamp

这是我的数据集

No. timestamp                    letter
1   2018-07-07 00:15:52          A
2   2018-07-07 09:55:34          A
3   2018-07-07 14:13:32          A
4   2018-07-08 02:22:51          A
5   2018-07-08 13:15:52          A
6   2018-07-08 18:52:43          A
7   2018-07-09 01:05:52          A
8   2018-07-09 09:15:52          A

我想在此数据中删除最近24小时的延迟时间戳,它是2018-07-08 09:15:52(最新时间戳之前24小时),所以问题是

No. timestamp                    letter
1   2018-07-07 00:15:52          A
2   2018-07-07 09:55:34          A
3   2018-07-07 14:13:32          A
4   2018-07-08 02:22:51          A

我应该怎么做

2 个答案:

答案 0 :(得分:3)

使用boolean indexing并从上一个/最大日期时间中减去1天:

 <!DOCTYPE html>
 <html>
 <head>
 <link href="https://cdn.botframework.com/botframework- 
      webchat/latest/botchat.css" rel="stylesheet" />
    </head>
  <body>
   <div id="bot" />
    <script src="https://cdn.botframework.com/botframework- 
   webchat/latest/botchat.js"></script>
    <script src="https://cdn.botframework.com/botframework- 
    webchat/latest/CognitiveServices.js"></script>
  <script>
  var speechOptions = {
    speechRecognizer: new CognitiveServices.SpeechRecognizer(
        { subscriptionKey: 'xxxxxxxx' }),

    speechSynthesizer: new CognitiveServices.SpeechSynthesizer(
        {
            subscriptionKey: 'xxxxxxxx',
            gender: CognitiveServices.SynthesisGender.Female,
            voiceName: 'Microsoft Server Speech Text to Speech Voice (en-US, 
     JessaRUS)'
        })
}

var botConnection = new BotChat.DirectLine({

    secret:"yyyyyyyyyyyyyyy",
    webSocket: true,
});


BotChat.App({
        speechOptions: speechOptions,
        botConnection: botConnection,
        user: {
            id: 'userid',
            name: 'User',
        },
        bot: { id: 'botid' },
        resize: 'detect',
        locale: 'en-US'

    },

function postHelp() {
    botConnection
        .postActivity({
            from: { id: 'userid', name: 'User' },
            name: 'postHelp',
            type: 'message',
            text: 'help'
        })
        .subscribe(function(id) {
            console.log('"postHelp" sent');
        });
};

function welcome() {
    botConnection
        .postActivity({
            from: { id: 'userid', name: 'User' },
            name: 'welcome',
            type: 'event',
            value: 'help'
        })
        .subscribe(function (id) {
            console.log('"welcome" sent');
        });
}
</script>

或者:

df = df[df['timestamp'] < df['timestamp'].iloc[-1] - pd.Timedelta(1, unit='d')]

df = df[df['timestamp'] < df['timestamp'].max() - pd.Timedelta(1, unit='d')]

答案 1 :(得分:1)

您可以使用pd.DateOffset从最长日期中减去一天。然后使用布尔掩码。

# convert to datetime
df['timestamp'] = pd.to_datetime(df['timestamp'])

# calculate max date permitted
max_date = df['timestamp'].max() - pd.DateOffset(days=1)

print(max_date)

2018-07-08 09:15:52

# apply Boolean mask
res = df[df['timestamp'] < max_date]

print(res)

   No.           timestamp letter
0    1 2018-07-07 00:15:52      A
1    2 2018-07-07 09:55:34      A
2    3 2018-07-07 14:13:32      A
3    4 2018-07-08 02:22:51      A