python-datetime

median of panda datetime64 column

∥☆過路亽.° 提交于 2019-12-30 09:30:08
问题 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

computing the mean for python datetime

别说谁变了你拦得住时间么 提交于 2019-12-29 08:41:33
问题 I have a datetime attribute: d = { 'DOB': pd.Series([ datetime.datetime(2014, 7, 9), datetime.datetime(2014, 7, 15), np.datetime64('NaT') ], index=['a', 'b', 'c']) } df_test = pd.DataFrame(d) I would like to compute the mean for that attribute. Running mean() causes an error: TypeError: reduction operation 'mean' not allowed for this dtype I also tried the solution proposed elsewhere. It doesn't work as running the function proposed there causes OverflowError: Python int too large to convert

Default “future” year when effecting date conversion with dateutil

懵懂的女人 提交于 2019-12-25 11:50:53
问题 The following date patterns 1st January 30th April are easily parsed into instances of datetime.date via dateutil.parser.parse() : In [1]:from dateutil.parser import parse In [2]: parse('1st January') Out[2]: datetime.datetime(2012, 1, 1, 0, 0) In [3]: parse('8th April') Out[3]: datetime.datetime(2012, 4, 30, 0, 0) How can a future date be returned from parsing? I.e. parsing '1st January' would return datetime.datetime(2013, 1, 1, 0, 0) , 1st January 2013 and not 1st January 2012. Any elegant

Group objects by dates

一曲冷凌霜 提交于 2019-12-25 07:58:19
问题 clicks = SellerClick.objects.extra({'date' : "date(timestamp)"}).values('date').annotate(count=Count('timestamp')) The model has a datetime field called timestamp that was are using. I first, convert the datetime field to just a date field. Then the rest is guessing. I need to group by, and then count how many objects are of each date. So the desired result would be a date, then a count, based on how many objects have that date in the timestamp field. 回答1: I prefer to use annotate over extra

Group objects by dates

安稳与你 提交于 2019-12-25 07:58:05
问题 clicks = SellerClick.objects.extra({'date' : "date(timestamp)"}).values('date').annotate(count=Count('timestamp')) The model has a datetime field called timestamp that was are using. I first, convert the datetime field to just a date field. Then the rest is guessing. I need to group by, and then count how many objects are of each date. So the desired result would be a date, then a count, based on how many objects have that date in the timestamp field. 回答1: I prefer to use annotate over extra

How to get the millisecond part while converting to date time from epoch using python

巧了我就是萌 提交于 2019-12-25 01:53:40
问题 I have a unix epoc time as 1571205166751 I want to convert it to date time in millisecond The desired result should be 2019-10-16 05:52:46:751 AM But using following code I am getting the result as 2019-10-16 05:52:46, not the millisecond part. import time time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(1571205166)) How to get the millisecond part while converting ? P.S. I want to covert epoch time, not any other format. 回答1: Use datetime.datetime.fromtimestamp epoch_time = 1571205166751 dt =

Get tz offset from string

删除回忆录丶 提交于 2019-12-24 17:06:05
问题 I have a date which is in local time: date: "2013-12-02 22:00:00" and another value the tz: timezone_offset: "GMT-0800" If I : dateutil.parser.parse(date).isoformat() I will get: "2013-12-02T22:00:00+0000" I want to implement the date in ISO format with the tz info and get a result of: "2013-12-02T22:00:00-0800" Something close to: parse(date,tzinfos=??).isoformat() ? How can I get the tzinfo from the string timezone_offset ? 回答1: >>> from dateutil.parser import parse >>> dt = parse("2013-12

Get tz offset from string

亡梦爱人 提交于 2019-12-24 17:04:28
问题 I have a date which is in local time: date: "2013-12-02 22:00:00" and another value the tz: timezone_offset: "GMT-0800" If I : dateutil.parser.parse(date).isoformat() I will get: "2013-12-02T22:00:00+0000" I want to implement the date in ISO format with the tz info and get a result of: "2013-12-02T22:00:00-0800" Something close to: parse(date,tzinfos=??).isoformat() ? How can I get the tzinfo from the string timezone_offset ? 回答1: >>> from dateutil.parser import parse >>> dt = parse("2013-12

How to do cumsum based on a time condition - resample pandas?

会有一股神秘感。 提交于 2019-12-23 23:20:00
问题 I have a dataframe like as shown below df = pd.DataFrame({ 'subject_id':[1,1,1,1,1,1], 'time_1' :['2173-04-03 10:00:00','2173-04-03 10:15:00','2173-04-03 10:30:00','2173-04-03 10:45:00','2173-04-03 11:05:00','2173- 04-03 11:15:00'], 'val' :[5,6,5,6,6,6] }) I would like to find the total duration of a value appearing in sequence. Below example will help you understand From the above screenshot, you can see that 6 occurs in sequence from 10:45 to 23:59 whereas other values (it could be any

pandas convert string columns to datetime, allowing missing but not invalid

那年仲夏 提交于 2019-12-23 20:01:28
问题 I have a pandas data frame with multiple columns of strings representing dates, with empty strings representing missing dates. For example import numpy as np import pandas as pd # expected date format is 'm/%d/%Y' custId = np.array(list(range(1,6))) eventDate = np.array(["06/10/1992","08/24/2012","04/24/2015","","10/14/2009"]) registerDate = np.array(["06/08/2002","08/20/2012","04/20/2015","","10/10/2009"]) # both date columns of dfGood should convert to datetime without error dfGood = pd