我有一个CSV文件,而且我只提取时间作为单独的列。我尝试了几种方法,但无法正常进行。我使用这个时间栏来构建一个线图。有什么想法吗?
日期列格式为: 2016-04-15 06:00:01.704
所以我的时间专栏最终应该是: 06:00
我的数据集外观示例:
Date x1 x2 .....
2016-04-15 06:00:31.678 5.10165404238 12.2763408161,.....
2016-04-15 06:01:01.670 2.12747098266 10.9392058608,....
2016-04-15 06:01:31.675 3.8868992871 ....
这就是我打算接近的方式: 每天绘制每分钟的折线图。 x轴每15分钟滴答一次 每个图形在x轴上都有时间,在y轴上有一列
我可以循环它们,但如何实施似乎是一个挑战
我必须绘制
答案 0 :(得分:0)
您应该将日期解析为结构,以便您可以轻松获得所有部分(小时,日,年,分钟等)。 Datetime.datetime.strptime是你的朋友。
答案 1 :(得分:0)
如果"日期列格式"是CSV文件每行中的唯一数据,如果您已读入变量s
,则可以使用
time_column = s[11:16]
这将按照您所述的时间给出时间。然而,这会缩短时间,而不是最近的分钟。为了圆,你可以在分钟后的冒号后检查字符。
答案 2 :(得分:0)
首先使用datetime
将时间戳转换为python datetime.strptime(date_string, format)import datetime
list_string = ['2016-04-15 06:00:01.704']
fmt = '%Y-%m-%d %H:%M:%S.%f'
list_datetimes = datetime.datetime.strptime(list_string, fmt)
然后使用date2num将日期时间转换为matplotlib格式。
import matplotlib
x = matplotlib.dates.date2num(list_datetimes)
现在,您可以使用plot_date
进行绘图import matplotlib.pyplot as plt
plt.plot_date(x, y)
使用xlim(...)限制x轴。
plt.xlim(xmax=...)
答案 3 :(得分:0)
对于Python中的日期和时间操作,我经常使用箭头库。我觉得使用它比使用datetime或tz更直观(即使箭头是建立在两者之上的,还有其他的)。
您可以在https://github.com/crsmithdev/arrow找到包含API的git页面,当然您也可以使用
进行安装。public abstract class BaseFragment extends Fragment {
private static HashMap<String, Object> sObjects;
public void onViewCreated(View view, Bundle savedInstanceState) {
if (savedInstanceState == null) {
Object o = new Object();
sObjects.put(getClass().getName(), o);
}
}
protected final Object getObject() {
return sObjects.get(getClass().getName());
}
}
然后你应该能够做到:
pip install arrow
我看到时间已经过了几个小时,这可能是因为它是在UTC中。呼叫
import arrow
arrow_object = arrow.get('2016-04-15 06:00:01.704')
time_str = arrow_object.format('HH:mm')
在将时间转换为字符串之前。有关更多信息,请查看API文档。他们相当不错!