【问题标题】:How to search my list of time data如何搜索我的时间数据列表
【发布时间】:2019-01-29 22:18:06
【问题描述】:

我在下面有工作代码。目前我正在从 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')

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    如果您的数据集是一致的,那么 pandas 可以为您修剪列。结帐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?

    将系列转换为日期

    【讨论】:

      【解决方案2】:

      也许尝试将您的 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' 转换为 datetime 对象

      这里

      %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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多