如何搜索我的时间数据列表

时间:2018-08-23 13:51:00

标签: python pandas matplotlib

我下面有工作代码。目前,我正在从sav文件提取数据,将其导出到csv文件,然后绘制此数据。它看起来不错,但我想放大它,但我不确定如何做到。这是因为我的时间以以下格式列出:

20141107B205309Y

代码中同时包含字母和数字,因此我不确定该怎么做。

我想我可以通过两种方式做到这一点:

  1. 我正在考虑使用python来“修剪”时间数据,以便它在csv文件中仅显示“ 20141107”,这将使其易于浏览。

  2. 我不确定是否可能,但是否有人知道我可以像通常使用数据一样使用“ xrange = []”来搜索代码。

我的代码:

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')

2 个答案:

答案 0 :(得分:1)

如果您的数据集一致,则熊猫可以为您修剪列。结帐https://pandas.pydata.org/pandas-docs/stable/text.html。您可以使用'B'字符进行分割。之后,将列转换为日期。 您可以使用How do I convert dates in a Pandas data frame to a 'date' data type?

将系列转换为日期

答案 1 :(得分:0)

也许尝试将s [“ time”]转换为日期时间对象列表,而不是字符串。

    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)

这里的str对象使用这种格式'%Y%m%dB%H%M%SY'转换为日期时间对象

这里

%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