In [1]:
import bson.json_util as bju
import json
In [2]:
%matplotlib inline
In [3]:
import pandas as pd
import numpy as np
import matplotlib.pylab as pl
In [4]:
n = 10
x = pd.date_range(start='2015-07-13 9:00:00', freq='S', periods=n)
y = np.vstack([range(n), range(n+1,1+2*n), range(1,2*n+1,2)]).T
df = pd.DataFrame(y, index=x)
In [5]:
df.plot()
Out[5]:
In [6]:
def PandasResample(df, length):
td = (df.index[-1] - df.index[0]) / (length-1)
return df.resample(td, how='mean').interpolate() # Handle NaNs when upsampling
In [7]:
df
Out[7]:
In [8]:
df.resample('500ms', fill_method='bfill')
Out[8]:
In [9]:
new_index = pd.date_range(start=df.index[0], end=df.index[-1], freq='500ms')
In [10]:
df.reindex(new_index).interpolate().plot()
Out[10]:
In [11]:
entries_oct_20 = json.load(open("power_drain/data/location/shankari_2015-10-20"), object_hook=bju.object_hook)
entries_oct_21 = json.load(open("power_drain/data/location/shankari_2015-10-21"), object_hook=bju.object_hook)
entries = []
entries.extend(entries_oct_20)
entries.extend(entries_oct_21)
In [12]:
locations = [entry for entry in entries if entry["metadata"]["key"] == "background/location"]
In [13]:
def expand_vals(location):
ret_dict = {}
ret_dict.update(location["metadata"])
ret_dict.update(location["data"])
return ret_dict
In [14]:
locations_df = pd.DataFrame([expand_vals(loc) for loc in locations]).set_index("local_dt")
In [15]:
locations_df.head()
Out[15]:
In [16]:
locations_df.latitude.plot()
Out[16]:
In [41]:
import datetime as pydt
import pytz
In [62]:
to_local_dt = lambda(ts): pydt.datetime.fromtimestamp(ts).replace(tzinfo=locations_df.index[0].tz)
In [63]:
new_index_oct20_first = pd.date_range(start=to_local_dt(1445349600), end=to_local_dt(1445371200), freq='1S')
In [64]:
new_index_oct20_second = pd.date_range(start=to_local_dt(1445373000), end=to_local_dt(1445394600), freq='1S')
In [65]:
len(locations_df.index.unique())
Out[65]:
In [66]:
len(locations_df)
Out[66]:
In [67]:
val_counts = locations_df.index.value_counts()
In [68]:
val_counts
Out[68]:
In [69]:
locations_df = locations_df.drop_duplicates(['ts'])
In [70]:
locations_df.head()
Out[70]:
In [89]:
first_dc = locations_df.reindex(new_index_oct20_first, method='nearest').interpolate().reset_index()
In [86]:
second_dc = locations_df.reindex(new_index_oct20_second, method='nearest').interpolate().reset_index()
In [94]:
(first_dc.latitude - second_dc.latitude).plot()
Out[94]:
In [ ]: