python-datetime

Changing the formatting of a datetime axis in matplotlib

↘锁芯ラ 提交于 2019-11-26 13:41:24
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 desired formatting? # sample data import numpy as np import pandas as pd N = 30 drange = pd.date_range("2014

How do I round datetime column to nearest quarter hour

若如初见. 提交于 2019-11-26 13:02:08
问题 I have loaded a data file into a Python pandas dataframe. I has a datetime column of the format 2015-07-18 13:53:33.280 . What I need to do is create a new column that rounds this out to its nearest quarter hour. So, the date above will be rounded to 2015-07-18 13:45:00.000 . How do I do this in pandas? I tried using the solution from here, but get an \'Series\' object has no attribute \'year\' error. 回答1: Assuming that your series is made up of datetime objects, You need to use Series.apply

Changing the formatting of a datetime axis in matplotlib

北城以北 提交于 2019-11-26 02:37:45
问题 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

Convert a python UTC datetime to a local datetime using only python standard library?

拈花ヽ惹草 提交于 2019-11-26 01:43:12
问题 I have a python datetime instance that was created using datetime.utcnow() and persisted in database. For display, I would like to convert the datetime instance retrieved from the database to local datetime using the default local timezone (i.e., as if the datetime was created using datetime.now()). How can I convert the UTC datetime to a local datetime using only python standard library (e.g., no pytz dependency)? It seems one solution would be to use datetime.astimezone( tz ), but how would

Convert DataFrame column type from string to datetime, dd/mm/yyyy format

為{幸葍}努か 提交于 2019-11-26 01:38:43
问题 How can I convert a DataFrame column of strings (in dd/mm/yyyy format) to datetimes? 回答1: The easiest way is to use to_datetime: df['col'] = pd.to_datetime(df['col']) It also offers a dayfirst argument for European times (but beware this isn't strict). Here it is in action: In [11]: pd.to_datetime(pd.Series(['05/23/2005'])) Out[11]: 0 2005-05-23 00:00:00 dtype: datetime64[ns] You can pass a specific format: In [12]: pd.to_datetime(pd.Series(['05/23/2005']), format="%m/%d/%Y") Out[12]: 0 2005

Convert a python UTC datetime to a local datetime using only python standard library?

荒凉一梦 提交于 2019-11-26 00:29:15
I have a python datetime instance that was created using datetime.utcnow() and persisted in database. For display, I would like to convert the datetime instance retrieved from the database to local datetime using the default local timezone (i.e., as if the datetime was created using datetime.now()). How can I convert the UTC datetime to a local datetime using only python standard library (e.g., no pytz dependency)? It seems one solution would be to use datetime.astimezone( tz ), but how would you get the default local timezone? jfs In Python 3.3+: from datetime import datetime, timezone def