我不知道如何显示 numpy.datetime64 的单位。说:
t=np.datetime64(123456789, 'ms' )
有什么方法告诉我单位是“ms”?
我有一个 Pandas 数据框df,其中一列由datetime64,例如
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1471 entries, 0 to 2940
Data columns (total 2 columns):
date 1471 non-null values
id 1471 non-null values
dtypes: datetime64[ns](1), int64(1)
Run Code Online (Sandbox Code Playgroud)
我想df使用一天中的小时作为标准进行子采样(独立于 中的其他信息date)。例如,在伪代码中
df_sub = df[ (HOUR(df.date) > 8) & (HOUR(df.date) < 20) ]
Run Code Online (Sandbox Code Playgroud)
对于某些功能HOUR。
我想这个问题可以通过从datetime64到的初步转换来解决datetime。这可以更有效地处理吗?
我有一个整数,它是 UNIX 纪元之后的微秒数。(格林威治标准时间)
\n\n如何使用 astype 将 1349863207154117 转换为 pandas.Timestamp("2012-10-10T06:00:07.154117", tz=\xc2\xa8UTC\xc2\xa8) ?astype 的文档不是很详尽。我已经尝试过以下方法。
\n\nx = 1349863207154117\ndt64 = np.int64(x).astype("M8[us]")\nprint dt64\nRun Code Online (Sandbox Code Playgroud)\n\n返回:
\n\nnp.datetime64("2012-10-10T06:00:07.154117-0400")\nRun Code Online (Sandbox Code Playgroud)\n\nx = 1349863207154117\ndt64 = np.int64(x).astype("M8[us]")\nprint dt64\nRun Code Online (Sandbox Code Playgroud)\n 我想向np.datetime64已从包含年,月,日,小时和分钟的列的.csv文件读取的熊猫数据框中添加一列,并将其用作索引。我将单独的列组合在一起,以形成一列日期时间字符串。
import numpy as np
import pandas as pd
filename = 'test.csv'
df = pd.read_csv(filename, header=0, usecols = [2,3,4,5,6], names = ['y','m','d','h','min'],dtype = {'y':'str','m':'str','d':'str','h':'str','min':'str'}) #read csv file into df
df['datetimetext'] = (df['y']+'-'+df['m']+'-'+df['d']+' '+df['h']+':'+df['min']+':00')
Run Code Online (Sandbox Code Playgroud)
因此,数据框如下所示:
y m d h min datetimetext
0 1993 09 06 00 30 1993-09-06 00:30:00
1 1993 09 06 01 00 1993-09-06 01:00:00
2 1993 09 06 01 30 1993-09-06 01:30:00
3 1993 09 06 02 00 1993-09-06 02:00:00
4 1993 09 06 02 30 …Run Code Online (Sandbox Code Playgroud) 我正在编写一个检查excel文件的程序,如果今天的日期在excel文件的日期列中,我会解析它
我正在使用:
cur_date = datetime.today()
Run Code Online (Sandbox Code Playgroud)
为今天的约会.我正在检查今天是否在列中:
bool_val = cur_date in df['date'] #evaluates to false
Run Code Online (Sandbox Code Playgroud)
我确实知道今天的日期在相关文件中.该系列的dtype是datetime64 [ns]
此外,我只检查日期本身而不是之后的时间戳,如果这很重要的话.我这样做是为了使时间戳00:00:00:
cur_date = datetime.strptime(cur_date.strftime('%Y_%m_%d'), '%Y_%m_%d')
Run Code Online (Sandbox Code Playgroud)
打印后该对象的类型也是日期时间
我已经对熊猫数据框进行了切片。
end_date = df[-1:]['end']
type(end_date)
Out[4]: pandas.core.series.Series
end_date
Out[3]:
48173 2017-09-20 04:47:59
Name: end, dtype: datetime64[ns]
Run Code Online (Sandbox Code Playgroud)
48173并只获取2017-09-20 04:47:59字符串?我必须使用2017-09-20 04:47:59作为参数来调用 REST API ,所以我必须从 Pandasdatetime64系列中获取字符串。48173并只获取 datetime 对象 [类似的东西datetime.datetime.strptime('2017-09-20 04:47:59', '%Y-%m-%d %H:%M:%S')]。我需要它,因为稍后我将不得不检查是否'2017-09-20 04:47:59' < datetime.datetime(2017,1,9) 我只需要转换一个单元格值,而不是整列。如何进行这些转换?
我在 df 中有此列:
> df["time"]
0 2007-02-01 22:00:00+00:00
1 2007-02-01 22:00:00+00:00
2 2007-02-01 22:00:00+00:00
3 2007-02-01 22:00:00+00:00
4 2007-02-01 22:00:00+00:00
Run Code Online (Sandbox Code Playgroud)
我想用日、月和年创建三个新列,但我无法找到一种方法来提取time column.