【问题标题】:How to rearrange a date in python如何在python中重新排列日期
【发布时间】:2015-11-26 10:28:29
【问题描述】:

我在 pandas 数据框中有一列看起来像:

test1.Received
Out[9]: 
0      01/01/2015 17:25
1      02/01/2015 11:43
2      04/01/2015 18:21
3      07/01/2015 16:17
4      12/01/2015 20:12
5      14/01/2015 11:09
6      15/01/2015 16:05
7      16/01/2015 21:02
8      26/01/2015 03:00
9      27/01/2015 08:32
10     30/01/2015 11:52 

这将时间戳表示为日月年时分。我想将日期重新排列为年月日时分。所以它看起来像:

test1.Received
Out[9]: 
0      2015/01/01 17:25
1      2015/01/02 11:43
...

【问题讨论】:

  • 你想要它们作为字符串吗?
  • 作为字符串应该没问题。通过使用函数 pd.to_datetime 字符串应该再次变成 datetime64[ns]

标签: python datetime pandas


【解决方案1】:

只需使用pd.to_datetime:

In [33]:
import pandas as pd
pd.to_datetime(df['date'])
Out[33]:
index
0    2015-01-01 17:25:00
1    2015-02-01 11:43:00
2    2015-04-01 18:21:00
3    2015-07-01 16:17:00
4    2015-12-01 20:12:00
5    2015-01-14 11:09:00
6    2015-01-15 16:05:00
7    2015-01-16 21:02:00
8    2015-01-26 03:00:00
9    2015-01-27 08:32:00
10   2015-01-30 11:52:00
Name: date, dtype: datetime64[ns]

在你的情况下:

pd.to_datetime(test1['Received'])

应该可以工作

如果要更改显示格式则需要解析为日期时间然后apply`datetime.strftime:

In [35]:
import datetime as dt
pd.to_datetime(df['date']).apply(lambda x: dt.datetime.strftime(x, '%m/%d/%y %H:%M:%S'))

Out[35]:
index
0     01/01/15 17:25:00
1     02/01/15 11:43:00
2     04/01/15 18:21:00
3     07/01/15 16:17:00
4     12/01/15 20:12:00
5     01/14/15 11:09:00
6     01/15/15 16:05:00
7     01/16/15 21:02:00
8     01/26/15 03:00:00
9     01/27/15 08:32:00
10    01/30/15 11:52:00
Name: date, dtype: object

所以上面现在显示月/日/年,在你的情况下应该可以工作:

pd.to_datetime(test1['Received']).apply(lambda x: dt.datetime.strftime(x, '%y/%m/%d %H:%M:%S'))

编辑

看来您需要将参数dayfirst=True 传递给to_datetime

In [45]:
pd.to_datetime(df['date'], format('%d/%m/%y %H:%M:%S'), dayfirst=True).apply(lambda x: dt.datetime.strftime(x, '%m/%d/%y %H:%M:%S'))

Out[45]:
index
0     01/01/15 17:25:00
1     01/02/15 11:43:00
2     01/04/15 18:21:00
3     01/07/15 16:17:00
4     01/12/15 20:12:00
5     01/14/15 11:09:00
6     01/15/15 16:05:00
7     01/16/15 21:02:00
8     01/26/15 03:00:00
9     01/27/15 08:32:00
10    01/30/15 11:52:00
Name: date, dtype: object

【讨论】:

  • 我尝试了第一行代码,但得到了:TypeError:描述符“strftime”需要一个“datetime.date”对象,但收到了一个“str”。列的dtype是object
  • 您应该在帖子中输入数据的 dtype,如果它已经是 str,那么 pd.to_datetime 会很好地解析。
  • 问题是当我执行 pd.to_datetime 时,我得到的是年月日而不是年月日
  • 您需要临时转换回 str ,然后转换为 datetime 我将发布代码
  • 如您所见,有一个错误。如果结果 (Out[35]) 显示月/日/年,那么前两个字符应该是 01,而我们得到的行是 0 到 4 天的月年和其余的月日。此外,在第一个结果 (Out[33]) 中显示所谓的年月日,我们可以再次看到,从第 5 行开始,它改变了转向年月日的行为。
【解决方案2】:

您可以使用datetime 函数在字符串之间进行转换。

# converts to date 
datetime.strptime(date_string, 'DD/MM/YYYY HH:MM')  

# converts to your requested string format
datetime.strftime(date_string, "YYYY/MM/DD HH:MM:SS")  

【讨论】:

    【解决方案3】:

    Pandas 有这个内置的,你可以指定你的日期时间格式 http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html。 使用infer_datetime_format

    >>> import pandas as pd
    >>> i = pd.date_range('20000101',periods=100)
    >>> df = pd.DataFrame(dict(year = i.year, month = i.month, day = i.day))
    >>> pd.to_datetime(df.year*10000 + df.month*100 + df.day, format='%Y%m%d')
    0    2000-01-01
    1    2000-01-02
    ...
    98   2000-04-08
    99   2000-04-09
    Length: 100, dtype: datetime64[ns]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      相关资源
      最近更新 更多