Why is Python datetime time delta not found?

import time from datetime import datetime, date, time, timedelta dayDates = [] today = datetime.now() dayDates.append(today.strftime(“%m%d%Y”)) for i in range(0,14): day = today + datetime.timedelta(days=i) print day The error that you are getting says, that datetime has no attribute timedelta. It happens, because you have imported from datetime specific things. In order to access timedelta … Read more

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

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(est).strftime(fmt)) which will print: 2016-01-24 18:00:00 UTC+0000 2016-07-24 18:00:00 … Read more

Given a date range how can we break it up into N contiguous sub-intervals?

I would actually follow a different approach and rely on timedelta and date addition to determine the non-overlapping ranges Implementation def date_range(start, end, intv): from datetime import datetime start = datetime.strptime(start,”%Y%m%d”) end = datetime.strptime(end,”%Y%m%d”) diff = (end – start ) / intv for i in range(intv): yield (start + diff * i).strftime(“%Y%m%d”) yield end.strftime(“%Y%m%d”) Execution … Read more

How can I get the first day of the next month in Python?

Here is a 1-line solution using nothing more than the standard datetime library: (dt.replace(day=1) + datetime.timedelta(days=32)).replace(day=1) Examples: >>> dt = datetime.datetime(2016, 2, 29) >>> print((dt.replace(day=1) + datetime.timedelta(days=32)).replace(day=1)) 2016-03-01 00:00:00 >>> dt = datetime.datetime(2019, 12, 31) >>> print((dt.replace(day=1) + datetime.timedelta(days=32)).replace(day=1)) 2020-01-01 00:00:00 >>> dt = datetime.datetime(2019, 12, 1) >>> print((dt.replace(day=1) + datetime.timedelta(days=32)).replace(day=1)) 2020-01-01 00:00:00

Changing the formatting of a datetime axis

import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates # sample data N = 30 drange = pd.date_range(“2014-01″, periods=N, freq=”MS”) np.random.seed(365) # for a reproducible example of values values = {‘values’:np.random.randint(1,20,size=N)} df = pd.DataFrame(values, index=drange) fig, ax = plt.subplots() ax.plot(df.index, df.values) ax.set_xticks(df.index) # use formatters to specify major … Read more

How can I translate dates and times from natural language to datetime? [closed]

parsedatetime – Python module that is able to parse ‘human readable’ date/time expressions. #!/usr/bin/env python from datetime import datetime import parsedatetime as pdt # $ pip install parsedatetime cal = pdt.Calendar() now = datetime.now() print(“now: %s” % now) for time_string in [“tomorrow at 6am”, “next moday at noon”, “2 min ago”, “3 weeks ago”, “1 … Read more

How to convert integer into date object python?

This question is already answered, but for the benefit of others looking at this question I’d like to add the following suggestion: Instead of doing the slicing yourself as suggested in the accepted answer, you might also use strptime() which is (IMHO) easier to read and perhaps the preferred way to do this conversion. import … Read more

How to make a datetime object aware (not naive)

In general, to make a naive datetime timezone-aware, use the localize method: import datetime import pytz unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0) aware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC) now_aware = pytz.utc.localize(unaware) assert aware == now_aware For the UTC timezone, it is not really necessary to use localize since … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)