2

I've got working code below. Currently I'm pulling data from sav files, exporting it to a csv file, and then plotting this data. It looks good, but I'd like to zoom in on it and I'm not entirely sure how to do it. This is because my time is listed in the following format:

20141107B205309Y

There are both letters and numbers within the code, so I'm not sure what to do.

I could do this two ways, I suppose:

  1. I was considering using python to "trim" the time data so it displays with only the "20141107" in the csv file, which should make it easy to browse.

  2. I'm not sure if its possible but if someone knows how I could obviously search the code using "xrange=[]" as I typically would with data.

My code:

import scipy.io as spio
import numpy as np
import csv
import pandas as pd
import matplotlib as plt

np.set_printoptions(threshold=np.nan)
onfile='/file'
finalfile='/fileout'

s=spio.readsav(onfile,python_dict=true,verbose=true)

time=np.asarray(s["time"])
data=np.asarray(s["data"])

d=pd.DataFrame({'time':time,'data':data})
d.to_csv(finalfile,sep=' ', encoding='utf-u',header=True)
d.plot(x='time',y='data',kind='line')
haramassive
  • 163
  • 1
  • 8

2 Answers2

1

If your data set is consistent then pandas can trim columns for you. Checkout https://pandas.pydata.org/pandas-docs/stable/text.html. You can split using 'B' character. After that convert the column into a date. You can convert a series to date using How do I convert dates in a Pandas data frame to a 'date' data type?

JR ibkr
  • 869
  • 7
  • 24
0

Maybe try converting your s["time"] into a list of datetime objects instead of string.

    from datetime import datetime
    date_list = [datetime.strptime(d, '%Y%m%dB%H%M%SY') for d in s["time"]]
    time=np.asarray(date_list)

Here str objects are converted into datetime objects using this format '%Y%m%dB%H%M%SY'

Here

%d is the day number
%m is the month number
%b is the month abbreviation
%y is the year last two digits
%Y is the all year
Nihal Sangeeth
  • 5,168
  • 2
  • 17
  • 32