python中的时空插值 [英] spatial-temporal interpolation in python

查看:72
本文介绍了python中的时空插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用Python的新手,所以我需要帮助.

I am new in using Python so I need a help.

我在四个列的两个DataFrame中有数据:纬度,经度,日期时间和温度.

I have data in two DataFrame in four columns: latitude, longitude, datetime and temperature.

在DataFrame df2中,我具有纬度,经度,日期时间,并且需要使用df1中的数据对温度进行插值.

In the DataFrame df2 I have latitude, longitude, datetime and I need to interpolate temperature using data from df1.

我需要使用坐标和日期时间数据进行插值,但我不知道该怎么做.

I need to use coordinate and datetime data to interpolate and I don't know how to do that.

DataFrame示例:

Example of DataFrame:

df1:

lat     |    lon   |      Datetime         | temp
---------------------------------------------------
15.13   |  38.52   |  2019-03-09 16:05:07  |   23

12.14   |  37.536  |  2019-03-15 09:50:07  |   22

13.215  |  39.86   |  2019-03-09 11:03:47  |   21

11.1214 |  38.536  |  2019-03-10 16:41:18  |   22

12.14   |  37.536  |  2019-03-09 06:15:27  |   19

df2:

lat     |     lon    |     Datetime           
---------------------------------------------
13.13   |   38.82    |   2019-03-06 04:05:07    
11.14   |   36.36152 |  2019-03-15 19:51:07      
10.214  |   39.123   |   2019-03-19 11:01:08    
12.14   |   37.536   |   2019-03-10 16:15:27    

我需要使用哪种方法或函数?

Which method or function I need to use?

推荐答案

处理时间插值的最佳方法是将时间转换成过去从参考点开始的总秒数.然后,您可以插值所有值,就好像它们是浮点数一样.

The best way to deal with a temporal interpolation is to convert the time into total seconds from a reference point in the past. You could then interpolate all values as though they were floats.

这是您输入的数据帧df1和df2:

Here are your input dataframes df1 and df2:

df1 = pd.DataFrame({'lat':[15.13,12.14,13.215,11.1214,12.14], 
              'lon': [38.52, 37.536,39.86,38.536,37.536],
              'Datetime': pd.to_datetime(['2019-03-09 16:05:07','2019-03-15 09:50:07','2019-03-09 11:03:47','2019-03-10 16:41:18','2019-03-09 06:15:27']),
              'temp':[23,22,21,22,19]})


df2 = pd.DataFrame({'lat':[13.13,11.14,10.214,12.14], 
              'lon': [38.82, 36.36152,39.123,37.536],
              'Datetime': pd.to_datetime(['2019-03-06 04:05:07 ','2019-03-15 19:51:07','2019-03-19 11:01:08','2019-03-10 16:15:27'])})

以下是根据过去参考点的秒数将时间转换为浮点数的方法:

Here is how you could convert time to floats, based on seconds from a reference point in the past:

df1['seconds'] = df1.Datetime.apply(lambda x: (pd.to_datetime(x)-pd.to_datetime('2019-03-01 00:00:00')).total_seconds())
df2['seconds'] = df2.Datetime.apply(lambda x: (pd.to_datetime(x)-pd.to_datetime('2019-03-01 00:00:00')).total_seconds())

最后,您可以使用scipy或任何其他包中的插值函数通过lat,lon和seconds列进行插值(请注意,df2中的某些点超出了df1中定义的范围,并且您得到的nans为结果):

And finally, you can use an interpolate function from scipy or any other package to interpolate using the lat, lon and seconds columns (note that some of your points in df2 fall outside the range defined in df1, and you get nans as a result):

from scipy.interpolate import griddata

griddata((df1.loc[:,['lat','lon','seconds']].values),
         df1.iloc[:,3].values,
         (df2.iloc[:,[0,1,3]].values))

这篇关于python中的时空插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆