python-datetime

python datetime.astimezone behavior incorrect?

爷,独闯天下 提交于 2019-12-23 16:32:08
问题 Here is the code which first parses time from string in IST and then converts that to UTC. So when it 4:00 pm in India the time in GMT / UTC is 10:30 am. While the following code prints it as 9:30 pm. So instead of subtracting the offset it is adding the offset. From the python documentation https://docs.python.org/2/library/datetime.html#datetime.datetime.astimezone the sample implementation of astimezone, it does appear that it would add the offset if it is negative but it seems contrary to

Equivalent of Excel's Weeknum function in Python

点点圈 提交于 2019-12-23 05:04:48
问题 I am working on automating a report which is presently handled in Excel. As part of this, I wanted a Python equivalent of Excel's Weeknum function (Using System 1. Reference here ) which considers the week having 1st Jan as Week 1. PS: I already tried ISOCalendar but it gives a wrong week as its week starts from Monday. I also tried the strftime("%U") and it returns the same wrong number. Can someone help on this? 回答1: Here is the pseudocode. You can make it into Python. You'll define a

Get week number using date in python

六眼飞鱼酱① 提交于 2019-12-22 22:40:40
问题 Date is datetime.date(2013, 12, 30) I am trying to get week number using import datetime datetime.date(2013, 12, 30).isocalendar()[1] I am getting output as , 1 Why i am not getting week number of last year , instead i am getting week number of current year? Whats wrong i am doing here ? 回答1: You are doing nothing wrong, 2013/12/30 falls in week 1 of 2014, according to the ISO8601 week numbering standard: The ISO 8601 definition for week 01 is the week with the year's first Thursday in it.

How to apply tz_convert with different timezones to different rows in pandas dataframe

∥☆過路亽.° 提交于 2019-12-22 10:57:28
问题 I am trying to set different timezones for various rows in a Pandas dataframe based on a criterion. As a MWE, here is what I have tried: test = pd.DataFrame( data = pd.to_datetime(['2015-03-30 20:12:32','2015-03-12 00:11:11']) ,columns=['time'] ) test['new_col']=['new','old'] test.time=test.set_index('time').index.tz_localize('UTC') test.loc[test.new_col=='new','time']=test[test.new_col=='new'].set_index('time').index.tz_convert('US/Pacific') print test The output of this: time new_col 0

How to apply tz_convert with different timezones to different rows in pandas dataframe

拈花ヽ惹草 提交于 2019-12-22 10:56:25
问题 I am trying to set different timezones for various rows in a Pandas dataframe based on a criterion. As a MWE, here is what I have tried: test = pd.DataFrame( data = pd.to_datetime(['2015-03-30 20:12:32','2015-03-12 00:11:11']) ,columns=['time'] ) test['new_col']=['new','old'] test.time=test.set_index('time').index.tz_localize('UTC') test.loc[test.new_col=='new','time']=test[test.new_col=='new'].set_index('time').index.tz_convert('US/Pacific') print test The output of this: time new_col 0

Is there an easy way to convert ISO 8601 duration to timedelta?

白昼怎懂夜的黑 提交于 2019-12-22 01:28:02
问题 Given a ISO 8601 duration string, how do I convert it into a datetime.timedelta ? This didn't work: timedelta("PT1H5M26S", "T%H%M%S") 回答1: I found isodate library to do exactly what I want isodate.parse_duration('PT1H5M26S') 来源: https://stackoverflow.com/questions/36976138/is-there-an-easy-way-to-convert-iso-8601-duration-to-timedelta

datetime to decimal hour and minutes in python3

女生的网名这么多〃 提交于 2019-12-20 01:13:07
问题 I have a dataframe with meteorological data every 30 minutes . With my datetime index I need to create a column with timestamps , but it must be in decimal . Here's the example below: In [134]: df.index[0:3] Out[134]: DatetimeIndex(['2016-01-01 00:30:00', '2016-01-01 01:00:00', '2016-01-01 01:30:00'], dtype='datetime64[ns]', name='date_time', freq=None) I need create a column as follows: df.new[0:3] 0.5,1,1.5 Where have 30 minutes i transform in .5 . Follow my script: import pandas as pd

How to handle times with a time zone in Matplotlib?

China☆狼群 提交于 2019-12-18 15:17:06
问题 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

Django DateTimeField Stores datetime regardless of the tzinfo

喜欢而已 提交于 2019-12-18 09:49:14
问题 Why django DateTimeField restore tzinfo in datetime to <utc> ? Below is my test code. Is it normal or wrong.? If it is normal, what is the reason? models.py class Date(models.Model): datetime = models.DateTimeField() settings.py TIME_ZONE = 'Asia/Seoul' USE_TZ = True test.py from django.utils import timezone datetime = timezone.localtime(timezone.localtimezone.now()) #now datetime is datetime.datetime(2015, 10, 22, 20, 31, 56, 248000, tzinfo=<DstTzInfo 'Asia/Seoul' KST+9:00:00 STD>) models

Whats wrong with my datetime.strptime format?

﹥>﹥吖頭↗ 提交于 2019-12-17 21:14:02
问题 With this function: timestamp = datetime.strptime(date_str, date_fmt) I am getting this error: ValueError: time data 'Sun, 28 Oct 2018 07:33:13 -0400 (EDT)' does not match format '%a, %d %b %Y %H:%M:%S %z (%Z)' Sun, 28 Oct 2018 07:33:13 -0400 (EDT) %a, %d %b %Y %H:%M:%S %z (%Z) I've looked over it a dozen times and I can't figure out what I am doing wrong. My Python Version: Python 3.7.0 (default, Jul 23 2018, 20:24:19) 回答1: There is no support for %Z on input for anything other than your