相关疑难解决方法(0)

在datetime,Timestamp和datetime64之间转换

如何将numpy.datetime64对象转换为datetime.datetime(或Timestamp)?

在以下代码中,我创建了datetime,timestamp和datetime64对象.

import datetime
import numpy as np
import pandas as pd
dt = datetime.datetime(2012, 5, 1)
# A strange way to extract a Timestamp object, there's surely a better way?
ts = pd.DatetimeIndex([dt])[0]
dt64 = np.datetime64(dt)

In [7]: dt
Out[7]: datetime.datetime(2012, 5, 1, 0, 0)

In [8]: ts
Out[8]: <Timestamp: 2012-05-01 00:00:00>

In [9]: dt64
Out[9]: numpy.datetime64('2012-05-01T01:00:00.000000+0100')
Run Code Online (Sandbox Code Playgroud)

注意:从时间戳中获取日期时间很容易:

In [10]: ts.to_datetime()
Out[10]: datetime.datetime(2012, 5, 1, 0, 0)
Run Code Online (Sandbox Code Playgroud)

但是我们如何从()中提取datetime或者?Timestampnumpy.datetime64dt64 …

python datetime numpy pandas

256
推荐指数
8
解决办法
35万
查看次数

Pandas 将时间戳列转换为日期时间

鉴于以下数据框和必要的争论:

import pandas as pd
df=pd.DataFrame({'A':['a','b','c'],
        'dates':['2015-08-31 00:00:00','2015-08-24 00:00:00','2015-08-25 00:00:00']})
df.dates=df.dates.astype(str)
df['dates'] = pd.to_datetime(df.dates.str.split(',\s*').str[0])
set(df['dates'])
Run Code Online (Sandbox Code Playgroud)

我最终得到:

{Timestamp('2015-08-24 00:00:00'),
 Timestamp('2015-08-25 00:00:00'),
 Timestamp('2015-08-31 00:00:00')}
Run Code Online (Sandbox Code Playgroud)

我需要将时间戳转换回日期时间(实际上,只是日期)格式。

我已经根据这篇文章的答案尝试过这个:

df['dates'].to_pydatetime()
Run Code Online (Sandbox Code Playgroud)

但这会返回:

AttributeError: 'Series' object has no attribute 'to_pydatetime'
Run Code Online (Sandbox Code Playgroud)

在我的真实数据中,数据类型是: <M8[ns]

datetime timestamp python-3.x

8
推荐指数
2
解决办法
3万
查看次数

迭代日期时间索引中的日期列表

我的初始数据框df

                     discharge1  discharge2
datetime                                   
2018-04-25 18:37:00        5862        4427
2018-04-25 21:36:30        6421        4581
2018-04-25 22:13:00        5948        4779
2018-04-26 00:11:30        5703        4314
2018-04-26 02:27:00        4988        3868
2018-04-26 04:28:30        4812        3823
2018-04-26 06:22:30        4347        3672
2018-04-26 10:50:30        3896        3546
2018-04-26 12:04:30        3478        3557
2018-04-26 14:02:30        3625        3598
2018-04-26 15:31:30        3751        3606
Run Code Online (Sandbox Code Playgroud)

我想要做的是让我的日期成为列表、数组或系列,我可以在其中迭代列表中的所有元素。这样我就可以使用这些日期来访问另一个数据框中的行df_other,最后将它们附加到一个新的数据框中df_new

for date in date_list():
    df_new = df_new.append(df_other.iloc[df_other.index.get_loc(date)])
Run Code Online (Sandbox Code Playgroud)

对于我列表中的日期应该运行为:

df_new.append(df_other.iloc[df_other.index.get_loc('2018-04-25 18:37:00')])
Run Code Online (Sandbox Code Playgroud)

我尝试使用df.index但返回一个列表Datetimeindex,我只能访问每个日期:

display(df.index[0])
Timestamp('2018-04-25 18:37:00')
Run Code Online (Sandbox Code Playgroud)

时间戳部分破坏了我的.append通话。

也尝试过df.index.tolist(),但会返回以下列表:[Timestamp('2018-04-25 …

python pandas

7
推荐指数
1
解决办法
1万
查看次数

转换日期:从年和周数转换为日期时间

我正在尝试转换以下数据框

id  year  week 
 1  2018    43
 1  2019     1
 2  2019     4
 3  2018    51
Run Code Online (Sandbox Code Playgroud)

到包含以下列的数据框中

id  year  week  year_week
 1  2018    43    2018-43
 1  2019     1     2019-1
 2  2019     4     2019-4
 3  2018    51    2018-51
Run Code Online (Sandbox Code Playgroud)

其中“year_week”是日期时间类型

python datetime week-number pandas

0
推荐指数
1
解决办法
2986
查看次数

标签 统计

datetime ×3

pandas ×3

python ×3

numpy ×1

python-3.x ×1

timestamp ×1

week-number ×1