python-datetime

How to handle times with a time zone in Matplotlib?

吃可爱长大的小学妹 提交于 2019-11-30 18:59:45
I have data points whose abscissas are datetime.datetime objects with a time zone (their tzinfo happens to be a bson.tz_util.FixedOffset obtained through MongoDB). When I plot them with scatter() , what is the time zone of the tick labels? Changing the timezone in matplotlibrc does not change anything in the displayed plot (I must have misunderstood the discussion on time zones in the Matplotlib documentation). I experimented a little with plot() (instead of scatter() ). When given a single date, it plots it and ignores the time zone. However, when given multiple dates, it uses a fixed time

how to plot time on y-axis in '%H:%M' format in matplotlib?

做~自己de王妃 提交于 2019-11-30 09:24:55
问题 i would like to plot the times from a datetime64 series, where the y-axis is formatted as '%H:%M, showing only 00:00, 01:00, 02:00, etc. this is what the plot looks like without customizing the y-axis formatting. import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotlib.dates import DateFormatter from matplotlib.dates import HourLocator df = pd.DataFrame(data=dict(a=pd.date_range('1/1/2011',periods=1440000,freq='1min'))) df = df.iloc[np.arange(0,1440*100,1440)+np

Convert a datetime.date object into a datetime.datetime object with zeros for any missing time attributes

假如想象 提交于 2019-11-30 09:23:41
问题 Is there a built-in function that converts a datetime.date object into a datetime.datetime object with 0's for the missing stuff? For example, suppose tdate = datetime.date(2012,1,31) I want to write something like either of these tdatetime = datetime.date.datetime() tdatetime = datetime.datetime(tdate) and I want the output to be datetime.datetime(2012, 1, 31, 0, 0) But neither works. There is a builtin function to go from datetime.datetime to datetime.date, but I'm looking for the reverse

Converting datetime string to datetime in numpy (python)

梦想的初衷 提交于 2019-11-30 08:17:23
问题 I would like to convert ['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"] into a Numpy datetime object. import numpy as np [np.datetime64(x) for x in ['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"]] raised ValueError: Could not convert object to NumPy datetime . However, the following works as I intended [np.datetime64(x) for x in ['2010-10-17 07:15:30', '2011-05-13 08:20:35', "2012-01-15 09:09:09"]] How can I convert my array into a format that

how to plot time on y-axis in '%H:%M' format in matplotlib?

做~自己de王妃 提交于 2019-11-29 15:29:45
i would like to plot the times from a datetime64 series, where the y-axis is formatted as '%H:%M, showing only 00:00, 01:00, 02:00, etc. this is what the plot looks like without customizing the y-axis formatting. import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotlib.dates import DateFormatter from matplotlib.dates import HourLocator df = pd.DataFrame(data=dict(a=pd.date_range('1/1/2011',periods=1440000,freq='1min'))) df = df.iloc[np.arange(0,1440*100,1440)+np.random.randint(1,300,100)] plt.plot(df.index,df['a'].dt.time) plt.show() After reading around on the

Convert a datetime.date object into a datetime.datetime object with zeros for any missing time attributes

限于喜欢 提交于 2019-11-29 14:47:39
Is there a built-in function that converts a datetime.date object into a datetime.datetime object with 0's for the missing stuff? For example, suppose tdate = datetime.date(2012,1,31) I want to write something like either of these tdatetime = datetime.date.datetime() tdatetime = datetime.datetime(tdate) and I want the output to be datetime.datetime(2012, 1, 31, 0, 0) But neither works. There is a builtin function to go from datetime.datetime to datetime.date, but I'm looking for the reverse operation. One very poor solution would be to write: datetime.datetime(tdate.year(), tdate.month(),

computing the mean for python datetime

好久不见. 提交于 2019-11-29 14:40:10
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 to C long What would you propose? The result for the above dataframe should be equivalent to datetime

Converting datetime string to datetime in numpy (python)

微笑、不失礼 提交于 2019-11-29 06:26:18
I would like to convert ['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"] into a Numpy datetime object. import numpy as np [np.datetime64(x) for x in ['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"]] raised ValueError: Could not convert object to NumPy datetime . However, the following works as I intended [np.datetime64(x) for x in ['2010-10-17 07:15:30', '2011-05-13 08:20:35', "2012-01-15 09:09:09"]] How can I convert my array into a format that conforms with Numpy 's datetime64 function requirement? I am using Numpy version 1.7.0. in python 3.4 So far

Convert date format python

爱⌒轻易说出口 提交于 2019-11-29 04:38:32
I have django form and I am receiving from POST a date formated like "%d/%m/%Y" and I would like to convert it to "%Y-%m-%d", How could I do it? Use strptime and strftime : In [1]: import datetime In [2]: datetime.datetime.strptime('10/05/2012', '%d/%m/%Y').strftime('%Y-%m-%d') Out[2]: '2012-05-10' Likewise, in Django template syntax you can use the date filter : {{ mydate|date:"Y-m-d" }} to print your date in your preferred format. One way is to use strptime and strftime : >>> import datetime >>> datetime.datetime.strptime('5/10/1955', '%d/%m/%Y').strftime('%Y-%m-%d') '1955-10-05' You can use

Given a date range how can we break it up into N contiguous sub-intervals?

六月ゝ 毕业季﹏ 提交于 2019-11-29 03:47:55
I am accessing some data through an API where I need to provide the date range for my request, ex. start='20100101', end='20150415'. I thought I would speed this up by breaking up the date range into non-overlapping intervals and use multiprocessing on each interval. My problem is that how I am breaking up the date range is not consistently giving me the expected result. Here is what I have done: from datetime import date begin = '20100101' end = '20101231' Suppose we wanted to break this up into quarters. First I change the string into dates: def get_yyyy_mm_dd(yyyymmdd): # given string