Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function for converting to a pandas dataframe #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions twincatscopeview/svbfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,16 @@ def interpolate(self, t):
return np.array(self.Values)

return np.interp(t, self.Time, self.Values)

def toDataFrame(self):
import pandas as pd
df = pd.DataFrame()
df['Time'] = self.Time
df[self.Name] = self.Values
return df.set_index('Time')

def __repr__(self):
return '[%s]*%d @ %0.2f ms: %.25s' % (self.DataType, self.SamplesInFile, self.SampleTime*1000, self.Name)
return '[%s]*%d @ %0.2f ms: %s' % (self.DataType, self.SamplesInFile, self.SampleTime*1000, self.Name)


class SVBFile(collections.abc.Mapping):
Expand Down Expand Up @@ -199,4 +206,22 @@ def __iter__(self):
return iter(self._channels)

def __len__(self):
return len(self._channels)
return len(self._channels)

def toDataFrame(self,keys=None,method='pad'):
'''Return a pandas dataframe containing data from channels with key in keys
df = svbFile.toDataFrame(keys=None,method='pad')
Backfills all missing data in lower frequency channels by pad method (details in pandas df.reindex)
'''
import pandas as pd

if keys is None:
keys = [self[a].Name for a in self]
lens = [self[a].SamplesInFile for a in self]
maxkey = keys[lens.index(max(lens))]
keys.remove(maxkey)

df = self[maxkey].toDataFrame()
for key in keys:
df = self[key].toDataFrame().reindex(df.index, method=method).join(df)
return df