python-datetime

Pandas: convert datetime timestamp to whether it's day or night?

白昼怎懂夜的黑 提交于 2019-12-08 09:39:30
问题 I am trying to determine if its a day or night based on list of timestamps. Will it be correct if I just check the hour between 7:00AM to 6:00PM to classify it as "day", otherwise "night"? Like I have done in below code. I am not sure of this because sometimes its day even after 6pm so whats the accurate way to differentiate between day or night using python? sample data: (timezone= utc/zulutime) timestamps = ['2015-03-25 21:15:00', '2015-06-27 18:24:00', '2015-06-27 18:22:00', '2015-06-27 18

Pandas DatetimeIndex converting dates to 1970

微笑、不失礼 提交于 2019-12-07 21:17:23
问题 I have recently faced a similar problem (answered here) whereby conversion of a date to a pandas DatetimeIndex and subsequent groupby using those dates led to an error where the date appeared as 1970-01-01 00:00:00+00:00 . I'm facing this problem in a different context now, and the previous solution isn't helping me. I have a frame like this import pandas as pd from dateutil import tz data = { 'Events' : range(1, 5 + 1 ,1), 'ID' : [1, 1, 1, 1, 1]} idx = pd.date_range(start='2008-01-01', end=

How to convert UTC to EST with Python and take care of daylight saving automatically?

空扰寡人 提交于 2019-12-07 13:21:14
问题 If I have a bunch of data with date & time in UTC format , how can I convert them to EST . It can determine when they will be -4(in summer) and - 5(in winter) automatically every year? Thanks 回答1: You'll need to use the pytz module (available from PyPI): import pytz from datetime import datetime est = pytz.timezone('US/Eastern') utc = pytz.utc fmt = '%Y-%m-%d %H:%M:%S %Z%z' winter = datetime(2016, 1, 24, 18, 0, 0, tzinfo=utc) summer = datetime(2016, 7, 24, 18, 0, 0, tzinfo=utc) print winter

get lastweek dates using python?

余生颓废 提交于 2019-12-07 06:20:00
问题 I am trying to get the date of the last week with python. if date is : 10 OCT 2014 means It should be print 10 OCT 2014, 09 OCT 2014, 08 OCT 2014, 07 OCT 2014, 06 OCT 2014, 05 OCT 2014, 04 OCT 2014 I tried: today = (10 OCT 2014) dates = [today + datetime.timedelta(days=i) for i in range(-4 - today.weekday(), 4 - today.weekday())] print dates I am getting this error: exceptions.AttributeError: 'unicode' object has no attribute 'weekday' 回答1: Your question and algorithm don't seem to correspond

Adding years in python

匆匆过客 提交于 2019-12-06 23:32:08
问题 If I want to add 100 years in my program, why is it showing the wrong date? import datetime stringDate= "January 10, 1920" dateObject= datetime.datetime.strptime(stringDate, "%B %d, %Y") endDate= dateObject+datetime.timedelta(days=100*365) print dateObject.date() print endDate.date() 回答1: The number of seconds in a year is not fixed. Think you know how many days are in a year? Think again. To perform period (calendar) arithmetic, you could use dateutil.relativedelta: #!/usr/bin/env python

Get week number using date in python

◇◆丶佛笑我妖孽 提交于 2019-12-06 08:17:14
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 ? 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. The Thursday in that week is 2014/01/02. Other ways to explain the definition, from the same linked WikiPedia

Build array of dates in last week, this week and next week

寵の児 提交于 2019-12-06 05:39:54
问题 I'm constantly tripping over things with regards to dates in Python. In my webapp I want to show every day of three weeks of a calendar: The last week, the current week and the following week, with Monday denoting the beginning of a week. The way I would currently approach this is stepping back through dates until I hit Monday and then subtract a further seven days and then add 20 to build the three-week range... But this feels really clunky. Does Python's have a concept of weeks or do I have

Pandas DatetimeIndex converting dates to 1970

被刻印的时光 ゝ 提交于 2019-12-06 05:15:20
I have recently faced a similar problem ( answered here ) whereby conversion of a date to a pandas DatetimeIndex and subsequent groupby using those dates led to an error where the date appeared as 1970-01-01 00:00:00+00:00 . I'm facing this problem in a different context now, and the previous solution isn't helping me. I have a frame like this import pandas as pd from dateutil import tz data = { 'Events' : range(1, 5 + 1 ,1), 'ID' : [1, 1, 1, 1, 1]} idx = pd.date_range(start='2008-01-01', end='2008-01-05', freq='D', tz=tz.tzlocal()) frame = pd.DataFrame(data, index=idx) Events ID 2008-01-01 00

How to get all datetime instances of the current week, given a day?

狂风中的少年 提交于 2019-12-06 04:34:16
问题 Given a day, I want to get all days(datetime instances) of the week in which day is present. I have a solution, please correct me if there is something wrong of if more efficient method exists. >>> import datetime >>> today = datetime.datetime(2013, 06, 26) >>> today datetime.datetime(2013, 6, 26, 0, 0) >>> day_of_week = today.isocalendar()[2] - 1 >>> day_of_week 2 >>> start_date = today - timedelta(days=day_of_week) >>> start_date datetime.datetime(2013, 6, 24, 0, 0) # Got monday >>> dates =

How to convert UTC to EST with Python and take care of daylight saving automatically?

时间秒杀一切 提交于 2019-12-05 21:30:42
If I have a bunch of data with date & time in UTC format , how can I convert them to EST . It can determine when they will be -4(in summer) and - 5(in winter) automatically every year? Thanks You'll need to use the pytz module (available from PyPI): import pytz from datetime import datetime est = pytz.timezone('US/Eastern') utc = pytz.utc fmt = '%Y-%m-%d %H:%M:%S %Z%z' winter = datetime(2016, 1, 24, 18, 0, 0, tzinfo=utc) summer = datetime(2016, 7, 24, 18, 0, 0, tzinfo=utc) print winter.strftime(fmt) print summer.strftime(fmt) print winter.astimezone(est).strftime(fmt) print summer.astimezone