excel插入一列日期 pandas_Pandas.dataframe.read_excel 读取excel 日期格式

本文介绍了在使用Pandas的`read_excel()`读取Excel文件时,如何处理日期格式。通过设置`dtype`、`parse_dates`和`date_parser`参数,尝试解析日期。默认使用dateutil.parser进行转换,可以自定义函数处理日期,或将日期存储为文本,以适应不同的需求。
摘要由CSDN通过智能技术生成

慕神8447489

老铁, 可以使用datetime库

import datetime

def time_convert(time_str):

time_obj = datetime.datetime.strptime(str(time_str), '%Y-%m-%d %H:%M:%S')

time_converted = time_obj.strftime('%Y/%m/%d')

return time_converted

dataset['日期'] = dataset['日期'].apply( time_convert )

于2019.2.24 12:27更改

简单的看了下官方的接口文档:有三处可以一试:

一.dtype参数

dtype : Type name or dict of column -> type, default None

Data type for data or columns. E.g. {'a': np.float64, 'b': np.int32}

Use `object` to preserve data as stored in Excel and not interpret dtype.

If converters are specified, they will be applied INSTEAD of dtype conversion.

dtype:类型名或(列 - >类型)的字典,默认为无        数据或列的数据类型 例如 {'a':np.float64,'b':np.int32}        使用object来保存存储在Excel中的数据,而不是解释dtype        如果指定了转换器,则它们将应用于dtype转换的INSTEAD。

而converters参数:

converters : dict, default None

Dict of functions for converting values in certain columns. Keys can

either be integers or column labels, values are functions that take one

input argument, the Excel cell content, and return the transformed

content.

converter:类型dict,默认无         需要一个用于转换某些列中的值的函数的字典。          键可以是整数或列标签,值是带有一个输入参数的函数,Excel单元格内容,并返回转换后的内容。

所以:你可以在read_excel()中设置:dtype={'data':str}, 而不是参数convert尝试过了, 无解。。。。。。

看了后面的文档, 好像是解释器, 默认会使用dateutil.parser.parser来解析你的时间, 并返回pd.Timestamp对象, 默认都是xxxx-xx-xx xx:xx:xx, 你可以重写pd.Timestamp的__str__() 和__repr__()函数

二.parse_dates 参数

parse_dates : bool, list-like, or dict, default False

The behavior is as follows:

* bool. If True -> try parsing the index.

* list of int or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column.

* list of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column.

* dict, e.g. {{'foo' : [1, 3]}} -> parse columns 1, 3 as date and call result 'foo'

If a column or index contains an unparseable date, the entire column or

index will be returned unaltered as an object data type. For non-standard

datetime parsing, use `pd.to_datetime` after `pd.read_csv`

就说下最后一段吧:

如果列或索引包含不可解析的日期,则整个列或索引将作为对象数据类型以不变的方式返回。 对于非标准的日期时间解析,在pd.read_csv()之后使用pd.to_datetime()

看完最后这个和时间有关参数, 我好像通了......

三.date_parser参数

date_parser : function, optionalFunction to use for converting a sequence of string columns to an array of datetime instances. The default uses dateutil.parser.parser to do the conversion. Pandas will try to call date_parser in three different ways, advancing to the next if an exception occurs:

1) Pass one or more arrays (as defined by `parse_dates`) as arguments;

2) concatenate (row-wise) the string values from the columns defined by `parse_dates` into a single array and pass that;

3) call `date_parser` once for each row using one or more strings (corresponding to the columns defined by `parse_dates`) as arguments.

翻译:

date_parser : function, optional该参数是需要一个用于将字符串列序列转换为日期时间实例数组的函数默认使用dateutil.parser.parser来进行转换。 Pandas将尝试以三种不同的方式调用date_parser,如果发生异常则前进到下一个:

1)传递一个或多个数组(由parse_dates定义)作为参数; 2)将来自parse_dates定义的列的字符串值连接(逐行)到一个数组中并传递; 3)使用一个或多个字符串(对应于parse_dates定义的列)作为参数,为每一行调用date_parser一次。

也就是说, 除非你制定一个对时间字符串不做改变的函数, 否则他一定会解析你的时间字符串转换成可以分析的格式(也就是你看到的那样“2018-12-31 00:00:00”),

你问的是:

为什么没有按照文本存储date

其实pandas是用来数据分析的, 时间得转换成特定的计算机能识别的格式, 计算机才可以帮我们识别分析, 如果以文本来存储, 占用内存资源大不说, 计算机也无法分析识别,

反过来说, 如果仅仅只是将时间保存为文本格式, 用excel也行呀, 用pandas有点大材小用的感觉

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值