datetime64

Pandas, dataframe with a datetime64 column, querying by hour

你说的曾经没有我的故事 提交于 2019-12-04 16:04:36
I have a pandas dataframe df which has one column constituted by datetime64 , e.g. <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) I would like to sub-sample df using as criterion the hour of the day (independently on the other information in date ). E.g., in pseudo code df_sub = df[ (HOUR(df.date) > 8) & (HOUR(df.date) < 20) ] for some function HOUR . I guess the problem can be solved via a preliminary conversion from datetime64 to datetime . Can this

How to force python print numpy datetime64 with specified timezone?

旧街凉风 提交于 2019-12-04 02:08:40
I want to see numpy datetime64 objects by my specified timezone. >>> import numpy as np >>> np.datetime64('2013-03-10T01:30:54') numpy.datetime64('2013-03-10T01:30:54+0400') >>> np.datetime64('2013-03-10T01:30:54+0300') numpy.datetime64('2013-03-10T02:30:54+0400') Python prints datetime objects always in UTC+0400 (it is my local timezone) even if I specify another timezone >>> np.datetime64('2013-03-10T01:30:54+0300') . Is there a way to force python print by UTC+0000 timezone? I am using numpy 1.8.1 . Mentioned a few times in the numpy documentation : The datetime object represents a single

median of panda datetime64 column

浪子不回头ぞ 提交于 2019-12-01 07:38:58
Is there a way to compute and return in datetime format the median of a datetime column? I want to calculate the median of a column in python which is in datetime64[ns] format. Below is a sample to the column: df['date'].head() 0 2017-05-08 13:25:13.342 1 2017-05-08 16:37:45.545 2 2017-01-12 11:08:04.021 3 2016-12-01 09:06:29.912 4 2016-06-08 03:16:40.422 Name: recency, dtype: datetime64[ns] My aim is to have the median in same datetime format as the date column above: Tried converting to np.array: median_ = np.median(np.array(df['date'])) But that throws the error: TypeError: ufunc add cannot

Extracting the first day of month of a datetime type column in pandas

。_饼干妹妹 提交于 2019-11-30 08:19:23
I have the following dataframe: user_id purchase_date 1 2015-01-23 14:05:21 2 2015-02-05 05:07:30 3 2015-02-18 17:08:51 4 2015-03-21 17:07:30 5 2015-03-11 18:32:56 6 2015-03-03 11:02:30 and purchase_date is a datetime64[ns] column. I need to add a new column df[month] that contains first day of the month of the purchase date: df['month'] 2015-01-01 2015-02-01 2015-02-01 2015-03-01 2015-03-01 2015-03-01 I'm looking for something like DATE_FORMAT(purchase_date, "%Y-%m-01") m in SQL. I have tried the following code: df['month']=df['purchase_date'].apply(lambda x : x.replace(day=1)) It works

Extracting the first day of month of a datetime type column in pandas

让人想犯罪 __ 提交于 2019-11-29 10:58:06
问题 I have the following dataframe: user_id purchase_date 1 2015-01-23 14:05:21 2 2015-02-05 05:07:30 3 2015-02-18 17:08:51 4 2015-03-21 17:07:30 5 2015-03-11 18:32:56 6 2015-03-03 11:02:30 and purchase_date is a datetime64[ns] column. I need to add a new column df[month] that contains first day of the month of the purchase date: df['month'] 2015-01-01 2015-02-01 2015-02-01 2015-03-01 2015-03-01 2015-03-01 I'm looking for something like DATE_FORMAT(purchase_date, "%Y-%m-01") m in SQL. I have

Difference between data type 'datetime64[ns]' and '<M8[ns]'?

妖精的绣舞 提交于 2019-11-28 18:09:27
I have created a TimeSeries in pandas: In [346]: from datetime import datetime In [347]: dates = [datetime(2011, 1, 2), datetime(2011, 1, 5), datetime(2011, 1, 7), .....: datetime(2011, 1, 8), datetime(2011, 1, 10), datetime(2011, 1, 12)] In [348]: ts = Series(np.random.randn(6), index=dates) In [349]: ts Out[349]: 2011-01-02 0.690002 2011-01-05 1.001543 2011-01-07 -0.503087 2011-01-08 -0.622274 2011-01-10 -0.921169 2011-01-12 -0.726213 I'm following on the example from 'Python for Data Analysis' book. In the following paragraph, the author checks the index type: In [353]: ts.index.dtype Out

Difference between data type &#39;datetime64[ns]&#39; and &#39;<M8[ns]&#39;?

跟風遠走 提交于 2019-11-26 15:42:49
问题 I have created a TimeSeries in pandas: In [346]: from datetime import datetime In [347]: dates = [datetime(2011, 1, 2), datetime(2011, 1, 5), datetime(2011, 1, 7), .....: datetime(2011, 1, 8), datetime(2011, 1, 10), datetime(2011, 1, 12)] In [348]: ts = Series(np.random.randn(6), index=dates) In [349]: ts Out[349]: 2011-01-02 0.690002 2011-01-05 1.001543 2011-01-07 -0.503087 2011-01-08 -0.622274 2011-01-10 -0.921169 2011-01-12 -0.726213 I'm following on the example from 'Python for Data