python-datetime

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

允我心安 提交于 2019-12-11 19:09:25
问题 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

Inconsistent internal representation of dates in matplotlib/pandas

守給你的承諾、 提交于 2019-12-11 17:28:24
问题 import pandas as pd index = pd.to_datetime(['2016-05-01', '2016-11-01', '2017-05-02']) data = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, index=index) ax = data.plot() print(ax.get_xlim()) # Out: (736066.7, 736469.3) Now, if we change the last date. index = pd.to_datetime(['2016-05-01', '2016-11-01', '2017-05-01']) data = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, index=index) ax = data.plot() print(ax.get_xlim()) # Out: (184.8, 189.2) The first example seems consistent with the

Syntax to use to make “minus x days” with Python 3.8?

拈花ヽ惹草 提交于 2019-12-11 16:59:14
问题 import pandas as pd import requests from bs4 import BeautifulSoup import time import datetime from datetime import date, timedelta # MM/DD/YYYY : time.strftime("%m/%d/%Y") # YYYYMMDD : time.strftime('%Y%m%d') if datetime.datetime.now().isoweekday() == 1 : # If today is a Monday -> some dates must be minus 3 days to take the previous Friday's data url_DAX = 'https://www.eurexchange.com/exchange-en/market-data/statistics/market-statistics-online/100!onlineStats?viewType=4&productGroupId=13394

Replace “tzinfo” and print with localtime amends six minutes

房东的猫 提交于 2019-12-11 08:36:30
问题 I am creating a DataTimeField no time zone. Soon I am editing this TZ "manually" and then asking to read the value with local TZ. See the end result amends six minutes! Logic: >>> import datetime >>> from django.utils import timezone >>> test = datetime.datetime(2016, 9, 28, 10, 10, 10) datetime.datetime(2016, 9, 28, 10, 10, 10) >>> test = teste.replace(tzinfo=pytz.timezone('America/Sao_Paulo')) datetime.datetime(2016, 9, 28, 10, 10, 10, tzinfo=<DstTzInfo 'America/Sao_Paulo' LMT-1 day, 20:54

User specified date time

有些话、适合烂在心里 提交于 2019-12-11 07:37:58
问题 I need to parse a date/time string from user input, and convert to UTC based on timzeone info not available in the string for datetime.strptime() (any suggestions?). Is there a straightforward way of doing this? Ideally, on google app engine i'd like to grab local time with tzinfo from the browser if possible also. timezone_string = "GMT-0800" fields = ("eventstartmonth","eventstartday", "eventstartyear", "eventstarttimehour", "eventstarttimeampm") date_string = '_'.join(map(lambda x: self

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

匆匆过客 提交于 2019-12-11 07:28:50
问题 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

convertion of datetime to numpy datetime without timezone info

独自空忆成欢 提交于 2019-12-11 02:07:03
问题 Suppose I have a datetime variable: dt = datetime.datetime(2001,1,1,0,0) and I convert it to numpy as follows numpy.datetime64(dt) I get numpy.datetime64('2000-12-31T19:00:00.000000-0500') with dtype('<M8[us]') But this automatically takes into account my time-zone (i.e. EST in this case) and gives me back a date of 2001-12-31 and a time of 19:00 hours. How can I convert it to datetime64[D] in numpy that ignores the timezone information and simply gives me numpy.datetime64('2001-01-01') with

How to select column for a specific time range from pandas dataframe in python3?

左心房为你撑大大i 提交于 2019-12-11 02:06:42
问题 This is my pandas dataframe time energy 0 2018-01-01 00:15:00 0.0000 1 2018-01-01 00:30:00 0.0000 2 2018-01-01 00:45:00 0.0000 3 2018-01-01 01:00:00 0.0000 4 2018-01-01 01:15:00 0.0000 5 2018-01-01 01:30:00 0.0000 6 2018-01-01 01:45:00 0.0000 7 2018-01-01 02:00:00 0.0000 8 2018-01-01 02:15:00 0.0000 9 2018-01-01 02:30:00 0.0000 10 2018-01-01 02:45:00 0.0000 11 2018-01-01 03:00:00 0.0000 12 2018-01-01 03:15:00 0.0000 13 2018-01-01 03:30:00 0.0000 14 2018-01-01 03:45:00 0.0000 15 2018-01-01 04

Need Sunday as the first day of the week

丶灬走出姿态 提交于 2019-12-10 17:54:10
问题 I would like Sunday to be 0, but when using the datetime weeday methods it's 6: datetime(2013, 6, 9, 11, 59, 59, 1).weekday() # this returns 6 My locale is: "en_US.UTF-8" so Sunday should be 0 (when I run 'locale day' at bash prompt, it correctly shows 'Sunday' as first day of the week). $ locale day Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday How can I get python to show Sunday as the first day of the week? (I'm using a pandas timeseries and calling .date to get the datetimes.)

Not able to convert “00:30 AM” to 24 hours by strptime python

我是研究僧i 提交于 2019-12-08 10:21:46
问题 I am trying to convert "00:30 AM" to 24 hours time but it gives ValueError: time data '00:30 AM' does not match format '%I:%M %p' The code used is: datetime.datetime.strptime('00:30 AM','%I:%M %p') Can somebody please help me solving this error? 回答1: The %I format expects a number between one and twelve, not zero and twelve. A 12 hour clock always uses 12 for midnight or noon. From the strftime() documentation: %I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, ..., 12 Either