python-datetime

how to apply ceiling to pandas DateTime

旧城冷巷雨未停 提交于 2019-12-05 00:14:29
问题 Suppose I have a pandas dataframe with a column whose values are datetime64[ns] . Out[204]: 0 2015-03-20 00:00:28 1 2015-03-20 00:01:44 2 2015-03-20 00:02:55 3 2015-03-20 00:03:39 4 2015-03-20 00:04:32 5 2015-03-20 00:05:52 6 2015-03-20 00:06:36 7 2015-03-20 00:07:44 8 2015-03-20 00:08:56 9 2015-03-20 00:09:47 Name: DateTime, dtype: datetime64[ns] Is there any easy way to convert them the nearest minute after the time? i.e. I want the following: Out[204]: 0 2015-03-20 00:01:00 1 2015-03-20 00

Python Pandas: TypeError: unsupported operand type(s) for +: 'datetime.time' and 'Timedelta'

独自空忆成欢 提交于 2019-12-04 12:14:24
问题 I am attempting to add two series in a dataframe in pandas with the first series being a 24-hr time value (e.g. 17:30) exported from an excel file and the second series being a series of the same length in Timedelta format converted from floats with the 'pd.Timedelta' command. The desired resulting third column would be a 24-hr time regardless of day change (e.g. 22:00 + 4 hours = 02:00). I created the Delta series like this: delta = pd.Series(0 for x in range(0, len(df.Time_In_Hours))) for j

How to get all datetime instances of the current week, given a day?

丶灬走出姿态 提交于 2019-12-04 11:15:29
Given a day, I want to get all days(datetime instances) of the week in which day is present. I have a solution, please correct me if there is something wrong of if more efficient method exists. >>> import datetime >>> today = datetime.datetime(2013, 06, 26) >>> today datetime.datetime(2013, 6, 26, 0, 0) >>> day_of_week = today.isocalendar()[2] - 1 >>> day_of_week 2 >>> start_date = today - timedelta(days=day_of_week) >>> start_date datetime.datetime(2013, 6, 24, 0, 0) # Got monday >>> dates = [start + timedelta(days=i) for i in range(7)] >>> dates [datetime.datetime(2013, 6, 24, 0, 0),

Build array of dates in last week, this week and next week

淺唱寂寞╮ 提交于 2019-12-04 10:38:59
I'm constantly tripping over things with regards to dates in Python. In my webapp I want to show every day of three weeks of a calendar: The last week, the current week and the following week, with Monday denoting the beginning of a week. The way I would currently approach this is stepping back through dates until I hit Monday and then subtract a further seven days and then add 20 to build the three-week range... But this feels really clunky. Does Python's have a concept of weeks or do I have to manually bodge it around with days? Edit: Now I code it out, it's not too horrific but I do wonder

Python select random date in current year

三世轮回 提交于 2019-12-04 04:24:39
In Python can you select a random date from a year. e.g. if the year was 2010 a date returned could be 15/06/2010 It's much simpler to use ordinal dates (according to which today's date is 734158): from datetime import date import random start_date = date.today().replace(day=1, month=1).toordinal() end_date = date.today().toordinal() random_day = date.fromordinal(random.randint(start_date, end_date)) This will fail for dates before 1AD. Not directly, but you could add a random number of days to January 1st. I guess the following should work for the Gregorian calendar: from datetime import date

Changing the formatting of a datetime axis in matplotlib

℡╲_俬逩灬. 提交于 2019-12-02 15:42:49
问题 I have a series whose index is datetime that I wish to plot. I want to plot the values of the series on the y axis and the index of the series on the x axis. The Series looks as follows: 2014-01-01 7 2014-02-01 8 2014-03-01 9 2014-04-01 8 ... I generate a graph using plt.plot(series.index, series.values) . But the graph looks like: The problem is that I would like to have only year and month. However, the graph contains hours, minutes and seconds. How can I remove them so that I get my

In Python how do I search for files created in the past 24 hours? [closed]

心不动则不痛 提交于 2019-12-02 14:44:48
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . I am new to python and I want to find all files in a directory that have been created within the past 24 hours. How do I filter the files that were created in 24 hour window. This code will be used in Python 2.7 on Windows computer. 回答1: Get the stat of the file then check if its

Convert incomplete 12h datetime-like strings into appropriate datetime type

ぐ巨炮叔叔 提交于 2019-12-02 07:53:34
I've got a pandas Series containing datetime-like strings with 12h format, but without the am/pm abbreviations. It covers an entire month of data : 40 01/01/2017 11:51:00 41 01/01/2017 11:51:05 42 01/01/2017 11:55:05 43 01/01/2017 11:55:10 44 01/01/2017 11:59:30 45 01/01/2017 11:59:35 46 02/01/2017 12:00:05 47 02/01/2017 12:00:10 48 02/01/2017 12:13:20 49 02/01/2017 12:13:25 50 02/01/2017 12:24:50 51 02/01/2017 12:24:55 52 02/01/2017 12:33:30 Name: TS, dtype: object (318621,) # shape My goal is to convert it to datetime format, so as to obtain the appropriate unix timestamps values, and make

Time zones and Localisation

折月煮酒 提交于 2019-12-01 09:57:54
问题 I'm currently storing all times in UTC, to make things easier for when I start bringing multiple sites and servers online. The problem comes in when translating date and datetime objects into strings in my templates and when accepting user input. 6:00PM UTC doesn't mean a whole lot to someone who is in PST. Likewise, asking users to input times in UTC is asking for disaster. How can I correctly translate these values in a smart, not-error-prone way? Is there a way I can determine from the

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