简体   繁体   English

如何更改整列的日期格式?

[英]How to change the date format of the whole column?

I am analyzing the .csv file and in this my first column is of the datetime in the format "2016-09-15T00:00:13" and I want to change this format to standard python datetime object.I can change the format for one but date but for whole column I can not do that. 我正在分析.csv文件,在此我的第一列是日期时间,格式为“ 2016-09-15T00:00:13”,我想将此格式更改为标准python datetime对象。我可以将格式更改为除日期外,我无法做到这一点。

My code that I am using: 我正在使用的代码:

import numpy
import dateutil.parser
mydate = dateutil.parser.parse(numpy.mydata[1:,0])
print(mydate)

I am getting the error: 我收到错误消息:

'module' object has no attribute 'mydata' “模块”对象没有属性“ mydata”

Here is the column for which I want the format to be changed. 这是我要更改格式的列。

print(mydata[1:,0])

['2016-09-15T00:00:13' 

'2016-09-15T00:00:38' 

'2016-09-15T00:00:53' 

...,

'2016-09-15T23:59:28' 

'2016-09-15T23:59:37' 

'2016-09-15T23:59:52']
from datetime import datetime

for date in mydata:
  date_object = datetime.strptime(date, '%Y-%m-%dT%H:%M:%S')

Here's a link to the method I'm using. 这是我正在使用的方法的链接 That same link also lists the format arguments. 该链接还列出了格式参数。

Oh and about the 哦,关于

'module' object has no attribute 'mydata' “模块”对象没有属性“ mydata”

You call numpy.mydata which is a reference to the "mydata" attribute of the numpy module you imported. 您调用numpy.mydata ,它是对导入的numpy模块的“ mydata”属性的引用。 The problem is, is that "mydata" is just one of your variables, not something included with numpy. 问题是“ mydata”只是您的变量之一,而不是numpy附带的变量。

Unless you have a compelling reason to avoid it, pandas is the way to go with this kind of analysis. 除非您有令人信服的理由避免这样做,否则熊猫就是进行这种分析的一种方式。 You can simply do 你可以简单地做

import pandas
df = pandas.read_csv('myfile.csv', parse_dates=True)

This will assume the first column is the index column and parse dates in it. 这将假定第一列是索引列并解析其中的日期。 This is probably what you want. 这可能就是您想要的。

Assuming you've dealt with that numpy.mydata[1:,0] attribute error 假设您已经处理了numpy.mydata[1:,0]属性错误

Your data looks like: 您的数据如下所示:

In [268]: mydata=['2016-09-15T00:00:13' ,
     ...: '2016-09-15T00:00:38' ,
     ...: '2016-09-15T00:00:53' ,
     ...: '2016-09-15T23:59:28' ,
     ...: '2016-09-15T23:59:37' ,
     ...: '2016-09-15T23:59:52']

or in array form it is a ld array of strings 或数组形式是字符串的ld数组

In [269]: mydata=np.array(mydata)
In [270]: mydata
Out[270]: 
array(['2016-09-15T00:00:13', '2016-09-15T00:00:38', '2016-09-15T00:00:53',
       '2016-09-15T23:59:28', '2016-09-15T23:59:37', '2016-09-15T23:59:52'], 
      dtype='<U19')

numpy has a version of datetime that stores as a 64 bit float, and can be used numerically. numpy具有datetime版本,该版本存储为64位浮点型,并且可以数字方式使用。 Your dates readily convert to that with astype (your format is standard): 您的日期很容易转换为带astype日期(您的格式是标准格式):

In [271]: mydata.astype(np.datetime64)
Out[271]: 
array(['2016-09-15T00:00:13', '2016-09-15T00:00:38', '2016-09-15T00:00:53',
       '2016-09-15T23:59:28', '2016-09-15T23:59:37', '2016-09-15T23:59:52'], 
       dtype='datetime64[s]')

tolist converts this array to a list - and the dates to datetime objects: tolist将此数组转换为列表-日期转换为datetime对象:

In [274]: D.tolist()
Out[274]: 
[datetime.datetime(2016, 9, 15, 0, 0, 13),
 datetime.datetime(2016, 9, 15, 0, 0, 38),
 datetime.datetime(2016, 9, 15, 0, 0, 53),
 datetime.datetime(2016, 9, 15, 23, 59, 28),
 datetime.datetime(2016, 9, 15, 23, 59, 37),
 datetime.datetime(2016, 9, 15, 23, 59, 52)]

which could be turned back into an array of dtype object: 可以将其转换回dtype对象的数组:

In [275]: np.array(D.tolist())
Out[275]: 
array([datetime.datetime(2016, 9, 15, 0, 0, 13),
       datetime.datetime(2016, 9, 15, 0, 0, 38),
       datetime.datetime(2016, 9, 15, 0, 0, 53),
       datetime.datetime(2016, 9, 15, 23, 59, 28),
       datetime.datetime(2016, 9, 15, 23, 59, 37),
       datetime.datetime(2016, 9, 15, 23, 59, 52)], dtype=object)

These objects couldn't be used in array calculations. 这些对象不能用于数组计算。 The list would be just as useful. 该列表将同样有用。

If your string format wasn't standard you'd have to use the datetime parser in a list comprehension as @staples shows. 如果您的字符串格式不是标准格式,则必须在列表@staples中使用datetime解析器,如@staples所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM