在python中,是否有一个函数来增加日期和时间,同时声明时间范围?

2024-05-29 02:00:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我们正在用python进行一个项目,在这个项目中,数据存储在MySQL数据库中。在数据库中,我们有两个表,其中表1(原始数据)从2个不同来源(LPN1,RisingHF)接收数据,表2(气象站)从一个来源存储数据。你知道吗

我们要做的是减去值,例如:

For 2019-07-26 15:00:00 to 2019-07-26 15:20:00
subtract latest data received from weather station with LPN1
subtract latest data received from weather station with RisingHF

For 2019-07-26 15:20:00 to 2019-07-26 15:40:00
subtract latest data received from weather station with LPN1
subtract latest data received from weather station with RisingHF

For 2019-07-26 15:40:00 to 2019-07-26 16:00:00
subtract latest data received from weather station with LPN1
subtract latest data received from weather station with RisingHF

等等。。。。。你知道吗

我们想也许我们可以增加时间和日期,例如:

import pandas as pd
from pandas.io import sql
from sqlalchemy import create_engine

list_lpn = []
list_rising = []

engine = create_engine("mysql://xxx:xxx@localhost/xxx")

conn = engine.connect()
    for x in range (24):
        for y in range (0, 20, 40):
            a = pd.read.sql("SELECT MAX(timestamp) FROM Weather_Station WHERE timestamp > 2019-07-26 x:y:00 AND timestamp < 2019-07-26 x:y+20:00, conn) 
            b = pd.read.sql("SELECT MAX(timestamp) FROM Raw_Data WHERE topic = 'lpn1' AND timestamp > 2019-07-26 x:y:00 AND timestamp < 2019-07-26 x:y+20:00, conn) 
            c = pd.read.sql("SELECT MAX(timestamp) FROM Raw_Data WHERE topic = 'RisingHF' AND timestamp > 2019-07-26 x:y:00 AND timestamp < 2019-07-26 x:y+20:00, conn) 

            list_lpn = a - b
            list_rising = a - c

这段代码还没有实现,但这是我们刚刚考虑过的,也许这就是如何做到的,但也许有更有效的方法来做到这一点?你知道吗

表1

  id | timestamp           | topic       | temperature | humidity | wind_speed | battery | secured | encrypted | correct |
+----+---------------------+-------------+-------------+----------+------------+---------+---------+-----------+---------+
| 16 | 2019-07-26 15:09:25 | lpn1        | 30.40       | 41.81    | NULL       | 3.13    |    NULL |      NULL |    NULL |
| 17 | 2019-07-26 15:11:30 | rhf1s001id1 | 29.61       | 43.32    | NULL       | 3.08    |    NULL |      NULL |    NULL |
| 18 | 2019-07-26 15:24:26 | lpn1        | 29.71       | 40.75    | NULL       | 3.13    |    NULL |      NULL |    NULL |
| 19 | 2019-07-26 15:25:25 | rhf1s001id1 | 29.60       | 42.34    | NULL       | 3.14    |    NULL |      NULL |    NULL |
| 20 | 2019-07-26 15:39:19 | rhf1s001id1 | 29.18       | 42.83    | NULL       | 3.15    |    NULL |      NULL |    NULL |
| 21 | 2019-07-26 15:39:26 | lpn1        | 29.46       | 40.89    | NULL       | 3.13    |    NULL |      NULL |    NULL |
| 22 | 2019-07-26 15:54:27 | lpn1        | 29.63       | 41.60    | NULL       | 3.14    |    NULL |      NULL |    NULL |
| 23 | 2019-07-26 15:55:38 | rhf1s001id1 | 28.73       | 44.29    | NULL       | 3.17    |    NULL |      NULL |    NULL |

表2

  id   | timestamp           | temperature1m | temperature1h | humidity1m | humidity1h |
+------+---------------------+---------------+---------------+------------+------------+
|    1 | 2019-07-26 15:00:13 |          26.9 |          28.3 |         44 |         48 |
|    2 | 2019-07-26 15:10:13 |            28 |          28.3 |         41 |         48 |
|    3 | 2019-07-26 15:20:13 |          27.8 |          28.3 |         39 |         48 |
|    4 | 2019-07-26 15:30:13 |            27 |          28.3 |         40 |         48 |
|    5 | 2019-07-26 15:40:13 |          26.8 |          28.3 |         42 |         48 |
|    6 | 2019-07-26 15:50:13 |            27 |          28.3 |         42 |         48 |
+------+---------------------+---------------+---------------+------------+------------+

Tags: andfromdatawithlatestnulltimestamppd

热门问题