I am working in python pandas and I am doing the following:
StDt = datetime(2018, 1, 1, 1, 0)
EnDt = datetime(2020, 1, 1, 1, 0)
allHours = pd.date_range(StDt, EnDt, freq='H').to_pydatetime()
The midnight hours are represented as:
datetime(2018, 1, 3, 0, 0)
datetime(2018, 1, 5, 0, 0)
Is it possible to create the series in a way such that midnight is represented as hour 24 of previous day
i.e. the above two cases will look as:
datetime(2018, 1, 2, 24, 0)
datetime(2018, 1, 4, 24, 0)
i.e. I am looking for following:
datetime(2018, 1, 3, 0, 0) = datetime(2018, 1, 2, 24, 0)
datetime(2018, 1, 5, 0, 0) = datetime(2018, 1, 4, 24, 0)
Edit:
My particular situation requires working in hour ending world and that is how the convention is in what I am working in.
Using datetimes, this is not possible. Python simply doesn't accept datetime(2018, 1, 2, 24, 0) as a valid time.
There was a request in 2010 to allow for this time to be accepted
Issue 10427: 24:-00 Hour in DateTime
which was rejected.
My only suggestion would be to consider whether you really need this time depicted as you outlined. For actual data manipulation, it should not make any difference as any operations you'd like to do in Pandas with datetimes will conform to this same restriction anyways.
I was working with similar data, and found it useful to consider that Hour Ending data labeled 1-24 is the equivalent of Hour Beginning data labeled 0-23.
So you'll have to change your rule set notation, but it should be a straightforward change.
Related
I have this code and I want to count the days between 2 dates.
from datetime import date, datetime
checkin= datetime(2022, 1, 30, 1, 15, 00)
checkout= datetime(2022, 1, 31, 0, 0, 00)
count_days= (checkout - checkin).days
In this case, the result of count_days result is 0, because in an operation with 2 datetimes, it takes into account hours, minutes and seconds.
I want the result to be 1 because is +1 day of difference. Type of variables must be datetimes. Thanks!
Convert them to dates first, with the date method.
from datetime import date, datetime
checkin = datetime(2022, 1, 30, 1, 15, 00)
checkout = datetime(2022, 1, 31, 0, 0, 00)
count_days = (checkout.date() - checkin.date()).days
Could you do something like this?
(assuming you want a minimum since the solution you have is similar)
from datetime import date, datetime
check_in= datetime(2022, 1, 30, 1, 15, 00)
check_out= datetime(2022, 1, 31, 0, 0, 00)
# Number of days between two dates (min 1 day)
days = (check_out - check_in).days + 1
print(days)
I am trying to get only month name and count number from
Data = QuerySet [{'month': datetime.datetime(2021, 3, 1, 0, 0, tzinfo=<DstTzInfo 'Asia/Dhaka' +06+6:00:00 STD>), 'count': 2 }]
If I use Data[0]['month'] result is showing like (2021, 3, 1,0,0, ...) but I need to see only as March
How can I view the month?
I'm assuming getting the total/count is not a big issue here.
Since you already have the datetime object ie.: (2021, 3, 1,0,0, ...) , you can extract the Month name using obj.strftime("%B")
Like this:
>>> date_obj
datetime.datetime(2021, 3, 1, 0, 0, tzinfo=<DstTzInfo 'Asia/Dhaka' LMT+6:02:00 STD>)
>>> month = date_obj.strftime("%B")
>>> month
'March'
You can also get short version of months, weekdays etc : Python documentation: strftime
I'm using rrule from python dateutil and don't know how to create an rruleset for the following example:
Monday, three weeks in a row. Then a week not, then again three weeks in a row, one not, and so on.
Any advice on creating an rrule(set) for this?
One way to do this is to use an rruleset with a WEEKLY rrule and a corresponding exrule for every 4th week:
from dateutil.rrule import rrule, rruleset
from dateutil.rrule import WEEKLY
from dateutil.relativedelta import relativedelta
from datetime import datetime, timedelta
dtstart = datetime(2011, 1, 1)
rrset = rruleset()
weekly_rule = rrule(freq=WEEKLY, dtstart=dtstart)
every_4_weeks = rrule(freq=WEEKLY, interval=4,
dtstart=dtstart + relativedelta(weeks=4))
rrset.rrule(weekly_rule)
rrset.exrule(every_4_weeks)
rrset.between(dtstart, dtstart + timedelta(days=65))
The result:
[datetime.datetime(2011, 1, 8, 0, 0),
datetime.datetime(2011, 1, 15, 0, 0),
datetime.datetime(2011, 1, 22, 0, 0),
datetime.datetime(2011, 2, 5, 0, 0),
datetime.datetime(2011, 2, 12, 0, 0),
datetime.datetime(2011, 2, 19, 0, 0),
datetime.datetime(2011, 3, 5, 0, 0)]
The way it works is weekly_rule generates one date per week, and the every_4_weeks generates every 4th week, starting with the 4th week after dtstart. That gives you a 3-on 1-off schedule.
Why do these two lines produce different results?
>>> import pytz
>>> from datetime import datetime
>>> local_tz = pytz.timezone("America/Los_Angeles")
>>> d1 = local_tz.localize(datetime(2015, 8, 1, 0, 0, 0, 0)) # line 1
>>> d2 = datetime(2015, 8, 1, 0, 0, 0, 0, local_tz) # line 2
>>> d1 == d2
False
What's the reason for the difference, and which should I use to localize a datetime?
When you create d2 = datetime(2015, 8, 1, 0, 0, 0, 0, local_tz), it does not handle daylight saving time (DST) correctly. local_tz.localize() does.
d1 is
>>> local_tz.localize(datetime(2015, 8, 1, 0, 0, 0, 0))
datetime.datetime(
2015, 8, 1, 0, 0,
tzinfo=<DstTzInfo 'America/Los_Angeles' PDT-1 day, 17:00:00 DST>
)
d2 is
>>> datetime(2015, 8, 1, 0, 0, 0, 0, local_tz)
datetime.datetime(
2015, 8, 1, 0, 0,
tzinfo=<DstTzInfo 'America/Los_Angeles' LMT-1 day, 16:07:00 STD>
)
You can see that they are not representing the same time.
d2 way is fine if you are going to work with UTC. UTC does not have daylight saving time (DST) transitions to deal with.
The correct way to handle timezone is to use local_tz.localize() to support daylight saving time (DST)
More information and additional examples can be found here:
http://pytz.sourceforge.net/#localized-times-and-date-arithmetic
I'm trying get a datetime instance representing April 23rd, but always get March 4th if the argument passed is '3.4', setting dayfirst=False is of no use:
In [115]: from dateutil import parser
In [116]: parser.parse('4-23', ) #√
Out[116]: datetime.datetime(2014, 4, 23, 0, 0)
In [117]: parser.parse('4/23', ) #√
Out[117]: datetime.datetime(2014, 4, 23, 0, 0)
In [118]: parser.parse('4.23', ) #×
Out[118]: datetime.datetime(2014, 3, 4, 0, 0)
In [120]: parser.parse('4.23', dayfirst=False) #×
Out[120]: datetime.datetime(2014, 3, 4, 0, 0)
is it a bug of parser?
The simple answer is that a dot character is not supported as a separator between units of time by the parse method since it is used in the context of a time string represented in ISO format.
Please try converting all dots to slashes(/) or heifens(-)
parser.parse('4.23'.replace('.','/'))
to resolve this problem
EDIT (to address new comments):
Here is an example of this in action
parser works as expected below:
>>> parser.parse('4/11/2019', dayfirst=True)
datetime.datetime(2019, 11, 4, 0, 0)
>>> parser.parse('4/11/2019', dayfirst=False)
datetime.datetime(2019, 4, 11, 0, 0)
parser assumes errors on behalf of the invoker and attempts to auto-correct the issue by invalidating the dayfirst parameter:
>>> parser.parse('3/13/2019', dayfirst=False)
datetime.datetime(2019, 3, 13, 0, 0)
>>> parser.parse('3/13/2019', dayfirst=True)
datetime.datetime(2019, 3, 13, 0, 0)
There cannot be a 13th month and therefore parser assumes that invoker incorrectly requested the dayfirst parameter - attempts to resolve the issue by invalidating the dayfirst instead of throwing an exception. This is another issue with this parser.