- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
日期在 python 中似乎是一件棘手的事情,我在简单地从 pandas 时间戳中删除日期时遇到了很多麻烦。我想从 2013-09-29 02:34:44
到简单的 09-29-2013
我有一个包含 Created_date 列的数据框:
Name: Created_Date, Length: 1162549, dtype: datetime64[ns]`
我已尝试在此系列上应用 .date()
方法,例如:df.Created_Date.date()
,但我收到错误 AttributeError :“系列”对象没有属性“日期”
谁能帮帮我?
最佳答案
map
覆盖元素:
In [239]: from operator import methodcaller
In [240]: s = Series(date_range(Timestamp('now'), periods=2))
In [241]: s
Out[241]:
0 2013-10-01 00:24:16
1 2013-10-02 00:24:16
dtype: datetime64[ns]
In [238]: s.map(lambda x: x.strftime('%d-%m-%Y'))
Out[238]:
0 01-10-2013
1 02-10-2013
dtype: object
In [242]: s.map(methodcaller('strftime', '%d-%m-%Y'))
Out[242]:
0 01-10-2013
1 02-10-2013
dtype: object
您可以通过调用构成 Timestamp
元素的 date()
方法来获取原始 datetime.date
对象>系列:
In [249]: s.map(methodcaller('date'))
Out[249]:
0 2013-10-01
1 2013-10-02
dtype: object
In [250]: s.map(methodcaller('date')).values
Out[250]:
array([datetime.date(2013, 10, 1), datetime.date(2013, 10, 2)], dtype=object)
另一种方法是调用未绑定(bind)的 Timestamp.date
方法:
In [273]: s.map(Timestamp.date)
Out[273]:
0 2013-10-01
1 2013-10-02
dtype: object
此方法最快,恕我直言,可读性最强。 Timestamp
可以在顶级 pandas
模块中访问,例如:pandas.Timestamp
。出于说明目的,我直接导入了它。
DatetimeIndex
对象的 date
属性做类似的事情,但返回一个 numpy
对象数组:
In [243]: index = DatetimeIndex(s)
In [244]: index
Out[244]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-10-01 00:24:16, 2013-10-02 00:24:16]
Length: 2, Freq: None, Timezone: None
In [246]: index.date
Out[246]:
array([datetime.date(2013, 10, 1), datetime.date(2013, 10, 2)], dtype=object)
对于较大的 datetime64[ns]
Series
对象,调用 Timestamp.date
比 operator.methodcaller
更快这比 lambda
:
In [263]: f = methodcaller('date')
In [264]: flam = lambda x: x.date()
In [265]: fmeth = Timestamp.date
In [266]: s2 = Series(date_range('20010101', periods=1000000, freq='T'))
In [267]: s2
Out[267]:
0 2001-01-01 00:00:00
1 2001-01-01 00:01:00
2 2001-01-01 00:02:00
3 2001-01-01 00:03:00
4 2001-01-01 00:04:00
5 2001-01-01 00:05:00
6 2001-01-01 00:06:00
7 2001-01-01 00:07:00
8 2001-01-01 00:08:00
9 2001-01-01 00:09:00
10 2001-01-01 00:10:00
11 2001-01-01 00:11:00
12 2001-01-01 00:12:00
13 2001-01-01 00:13:00
14 2001-01-01 00:14:00
...
999985 2002-11-26 10:25:00
999986 2002-11-26 10:26:00
999987 2002-11-26 10:27:00
999988 2002-11-26 10:28:00
999989 2002-11-26 10:29:00
999990 2002-11-26 10:30:00
999991 2002-11-26 10:31:00
999992 2002-11-26 10:32:00
999993 2002-11-26 10:33:00
999994 2002-11-26 10:34:00
999995 2002-11-26 10:35:00
999996 2002-11-26 10:36:00
999997 2002-11-26 10:37:00
999998 2002-11-26 10:38:00
999999 2002-11-26 10:39:00
Length: 1000000, dtype: datetime64[ns]
In [269]: timeit s2.map(f)
1 loops, best of 3: 1.04 s per loop
In [270]: timeit s2.map(flam)
1 loops, best of 3: 1.1 s per loop
In [271]: timeit s2.map(fmeth)
1 loops, best of 3: 968 ms per loop
请记住,pandas
的目标之一是在 numpy
之上提供一个层,以便(大多数时候)您不必处理 ndarray
的底层细节。因此,在数组中获取原始 datetime.date
对象的用途有限,因为它们不对应于 pandas
numpy.dtype
code>(pandas
仅支持 datetime64[ns]
[that's nanoseconds] dtypes)。也就是说,有时您需要这样做。
关于python - 从 pandas Timestamp 获取 MM-DD-YYYY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19105976/
我正在尝试使用这两种格式解析日期 2014-12-03T10:05:59.5646+08:00: yyyy-MM-dd'T'HH:mm:ss yyyy-MM-dd'T'HH:mm:ssXXX 当我使用
Paypal 返回以下格式的时间戳: yyyy-MM-ddTHH:mm:ssZ 我不知道该怎么办... 如何在 php 中使用本地时区将其转换为 yyyy-MM-dd HH:mm:ss? 我很想pre
我正在使用 Excel 2010 或 Excel 2007 导入包含日期/时间信息的 CSV 文件。我的 CSV 文件中的时间戳具有以下格式:yyyy-mm-dd hh:mm:ss。 (例如:2015
这个问题已经有答案了: Separate Date and Time objects (2 个回答) 已关闭 4 年前。 如何从 SimpleDateFormat("MM/dd/yyyy kk:mm"
这个问题已经有答案了: Java string to date conversion (17 个回答) 已关闭 6 年前。 我需要将日期字符串转换为另一种特定格式。 例如:我有一个日期,可以是 YYY
我想将字符串:24/11/2016 04:30 pm 转换为日期时间值:11/24/2016 04:30 pm。 我的代码为: DateTime date = DateTime.ParseExact(
我想使用 linux 将像“26/11/05 06:00:01,057000000”这样的纪元转换为 yyyy-mm-ddThh:mm:ss? 我曾尝试使用以下脚本但没有成功: echo 26/11/
这个问题在这里已经有了答案: mysql YYYY-MM-DDThh:mm:ss (1 个回答) 关闭 6 年前。 我想上传包含 yyyy-mm-ddThh:mm:ss.sssZ 数据的 csv 文
我在“dd-MM-yyyy HH:mm”中有一个字符串,需要将其转换为格式为日期的对象“yyyy-MM-dd HH:mm”。 下面是我用来转换的代码 oldScheduledDate = "16-05
我有一个数据框(df),它有一个日期列(列名:sale_date),它以以下格式存储数据 dd/mm/yy hh:mm:ss 我正在尝试将其转换为 yyyy-mm-dd hh:mm:ss。尝试了以下但
我的数据库中有日期时间列(格式为 YYYY-mm-dd hh:mm:ss)。 我需要将其转换为 dd-mm-YYYY hh:mm:ss 格式。 我该怎么办?帮助我。 最佳答案 不确定如何在 javas
想知道它们是否代表不同的格式或本质上相同(只是新与旧的演示文稿)。 最佳答案 基于DateTimeFormatter : Offset X and x: This formats the offset
如标题所示,我有一个问题。我需要将 LocalDataTime yyyy-MM-ssThh-mm-ss 解析为 LocalDataTime yyyy-MM-ss hh-mm-ss 但是当我这样做时 S
我想用 mySQL 将我的数据从“yyyy-mm-ddThh-mm-ss.sssZ”转换为“yyyy-mm-dd hh-mm-ss”。 我尝试使用 convert_tz: mysql> SELECT
这个问题在这里已经有了答案: LOAD DATA INFILE easily convert YYYYMMDD to YYYY-MM-DD? (1 个回答) 关闭 6 年前。 我正在尝试将 CSV
我需要更改 string 的日期格式。原始字符串的格式如下: var timeStamp = '2014/07/30 - 14:15:36' 这是我想要实现的日期格式: var timeStampAr
我正在尝试将 yyyy-MM-dd'T'HH:mm:ss.SSSz 格式的日期格式化为 yyyy-mm-dd HH:mm:ss,这应该很容易,但我无法获得它可以工作。 需要解析的日期格式为:2012-
我正在尝试通过传递时间戳作为命令行参数来使用 Synapse 管道运行 Spark 作业。在与 Spark 作业相关的代码运行之前,synapse 正在将字符串命令行参数值从 ISO 格式 2019-
我试过下面的代码: String created_Date = "25-Nov-15 14:23:34"; SimpleDateFormat sdf = new SimpleDateFormat("d
我创建了一个函数,它以与原始格式不同的格式返回日期。基本上,我正在使用此 Select MonthSub('2014-04-10',2)# 语句进行测试,它应该返回2014-02,而不是 2014-0
我是一名优秀的程序员,十分优秀!