Ad
2D Interpolation With Datetime Format X Values
I have a dataframe like this:
import pandas as pd
import numpy as np
time = pd.date_range('2018-05-14 00:00:00','2018-05-14 01:00:00',freq='5T')
mile = np.linspace(0,100,10)
x = list(time)*len(mile)
y = np.repeat(mile,len(time))
z = []
for i in range(0,10,1):
z.extend(np.random.normal(loc=i*5, scale=5, size=13))
origin_data = pd.DataFrame({'x':x, 'y':y ,'z':z})
origin_data
contains original points' positions(x and y) and their values(z). I want to interpolate the z
values at these new positions: x = pd.date_range('2018-05-14 00:00:00','2018-05-14 01:00:00',freq='1T')
with y = np.linspace(0,91,1)
just using bilinear interpolation.
I learned about the official document about scipy.interpolate.interp2d
. But its x type is numeric, mine is datetime. Also, the tutorial's z
values are calculated while mine are already given so I don't know how to handle the order of input z
value. Could anyone give me an example that contains an interpolation result plot based on the dataframe I provided above? Thank you for your attention!
Ad
Answer
This is the way I found to this question:
import pandas as pd
import numpy as np
from scipy import interpolate
import itertools
time = pd.date_range('2018-05-14 00:00:00','2018-05-14 01:00:00',freq='5T')
mile = np.arange(0,100,10)
x = list(time)*len(mile)
y = np.repeat(mile,len(time))
z = []
for i in range(0,10,1):
z.extend(np.random.normal(loc=i*5, scale=5, size=13))
origin_data = pd.DataFrame({'x':x, 'y':y ,'z':z})
from ggplot import *
ggplot(aes(x = 'x', y = 'y', colour = 'z'), data = origin_data) +\
geom_point(size = 100) +\
scale_x_date(labels = date_format("%Y-%m-%d %H:%M:S"))
x_numeric = [x.timestamp() for x in origin_data['x']]
x_cors = pd.unique(x_numeric)
y_cors = pd.unique(origin_data['y'])
cors = list(itertools.product(x_cors,y_cors))
interp_func = interpolate.LinearNDInterpolator(cors, z)
interp_func = interpolate.CloughTocher2DInterpolator(cors, z)
new_x = [x.timestamp() for x in pd.date_range('2018-05-14 00:00:00','2018-05-14 01:00:00',freq='1T')]
new_y = np.arange(0,91,1)
new_cors = list(itertools.product(new_x,new_y))
new_z = interp_func(new_cors)
new_data = pd.DataFrame({'x':[x[0] for x in new_cors],
'y':[x[1] for x in new_cors],
'z':new_z})
import datetime
new_data['x'] = [pd.Timestamp(x,unit = 's') for x in new_data['x']]
ggplot(aes(x='x',y='y',colour='z'),data=new_data) +\
geom_point(size=100) +\
scale_x_date(labels = date_format("%Y-%m-%d %H:%M:S"))
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad