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

在python中,是否有一个函数来增加日期和时间,同时声明时间范围?,python,mysql,sqlalchemy,Python,Mysql,Sqlalchemy,我们正在用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 rec

我们正在用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
等等

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

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
表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 |
+------+---------------------+---------------+---------------+------------+------------+

datetime.datetime()+datetime.timedelta()
在一次查询中是否只需要范围
15:00到15:20
或所有范围
:00到:20
?也许在SQL中,函数只给出了几分钟的时间,然后您可以得到
minutes(timestamp)>00和minutes(timestamp)<20
。但是每个数据库可能会使用不同的语法/函数,只需要几分钟。谢谢,Furas!在我们有数据的所有日子里,我们需要每天每20分钟减去这些值!我将研究上述问题。似乎很有趣,也许你应该使用普通整数
y
y+20
,然后像任何其他字符串一样格式化SQL
f{x}:00{x}:{y+20}:00
格式(x,y,x,y+20)
()+datetime.timedelta()在一个查询中是否只需要范围
15:00到15:20
或所有范围
:00到:20
?也许在SQL中是一个函数,它只给出时间的分钟数,然后您可以得到
分钟(时间戳)>00和分钟(时间戳)<20
。但是每个数据库可能使用不同的语法/函数只获得分钟。谢谢,Furas!我们需要在所有有数据的日子里每天每20分钟减去一次值!我将研究上面的内容。似乎很有趣。也许你应该使用普通整数
y
y+20
,然后像其他stri一样格式化SQLng
f{x}:{y}:00{x}:{y+20}:00“
“{}:{}:00{}:{}:00”。格式(x,y,x,y+20)
BTW:如果对循环使用
,则不需要
x++
y++