Questions tagged [python-datetime]

Python's built-in datetime module provides classes for manipulating dates and times in both simple and complex ways.

Python's built-in datetime module provides classes for manipulating dates and times in both simple and complex ways.

View on Python docs - for Python 2 and Python 3.

1880 questions
16
votes
2 answers

Converting datetime string to datetime in numpy (python)

I would like to convert ['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"] into a Numpy datetime object. import numpy as np [np.datetime64(x) for x in ['17-10-2010 07:15:30', '13-05-2011 08:20:35', "15-01-2013 09:09:09"]]…
Duna
  • 725
  • 2
  • 7
  • 16
15
votes
4 answers

How to require a timestamp to be zero-padded during validation in Python?

I'm trying to validate a string that's supposed to contain a timestamp in the format of ISO 8601 (commonly used in JSON). Python's strptime seems to be very forgiving when it comes to validating zero-padding, see code example below (note that the…
Niklas9
  • 8,816
  • 8
  • 37
  • 60
15
votes
2 answers

How to handle times with a time zone in Matplotlib?

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…
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
13
votes
1 answer

How can i check if date is on range on Python?

I'm trying to check if today's date (in dd-mm-yyyy format) is in a given range. My code only checks the day, not the month or year... Could you help me to see what's wrong? Here it works fine... import datetime TODAY_CHECK =…
Kushro
  • 133
  • 1
  • 1
  • 5
13
votes
2 answers

Pandas Assign Lambda Function

I have a DataFrame which has an open time and a close time and I am trying to calculate the difference in milliseconds. My code is currently like this df = df.assign(Latency=lambda d: d.CloseTimeStamp - d.CreationTimeStamp) df.Latency =…
aydow
  • 3,673
  • 2
  • 23
  • 40
13
votes
2 answers

Python: most efficient way to convert date to datetime

In Python, I convert a date to datetime by: converting from date to string converting from string to datetime Code: import datetime dt_format="%d%m%Y" my_date = datetime.date.today() datetime.datetime.strptime(my_date.strftime(dt_format),…
Joseph Victor Zammit
  • 14,760
  • 10
  • 76
  • 102
12
votes
3 answers

AttributeError: 'TimedeltaProperties' object has no attribute 'minute'

I have a dataframe that looks like this df [output]: date time 2020-02-28 00:30:45 2020-02-28 00:30:45 2020-03-09 00:21:06 2020-03-09 00:21:06 2020-03-09 00:21:06 with df.time.dtype [output]: dtype('
Le Noff
  • 345
  • 1
  • 2
  • 11
12
votes
3 answers

median of panda datetime64 column

Is there a way to compute and return in datetime format the median of a datetime column? I want to calculate the median of a column in python which is in datetime64[ns] format. Below is a sample to the column: df['date'].head() 0 2017-05-08…
T-Jay
  • 347
  • 1
  • 4
  • 16
12
votes
3 answers

timedelta error with numpy.longdouble dtype

I have times with dtype numpy.longdouble and when I'm trying to use that values with timedelta function I've got errors. But when I convert it to numpy.float64 everything is fine. Could somebody explain that behaviour? import numpy as np from…
Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93
12
votes
2 answers

SqlAlchemy filter by time difference

I'm new (but not so new) to SQLAlchemy. I'm using version 0.9.3 for a project. I want to make a query to the SQLite database filtering the result to get those objects with no time stamp or have more than 24 hours since their last update (on the same…
shackra
  • 277
  • 3
  • 16
  • 56
12
votes
2 answers

python compare datetimes with different timezones

I'm implementing feature with scheduled publishing of object. User chooses the time to publish and i created a cron task to run every minute and check if it's the time to publish. Users are from different timezones. So i need to compare two…
Feanor
  • 3,568
  • 5
  • 29
  • 49
11
votes
3 answers

Python datetime.now() as a default function parameter return same value in different time

Now I got some problem that I can't explain and fix. This is my first python module TimeHelper.py from datetime import datetime def fun1(currentTime = datetime.now()): print(currentTime) and another is Main.py from TimeHelper import…
EnergyBoy
  • 547
  • 1
  • 5
  • 18
11
votes
2 answers

Pandas: Rounding to nearest Hour

I have a column with timestamps start_time: 0 2016-06-04 05:18:49 1 2016-06-04 06:50:12 2 2016-06-04 08:16:02 3 2016-06-04 15:05:13 4 2016-06-04 15:24:25 I want use a function on the start_time column to round minutes >= 30 to…
EJSuh
  • 195
  • 1
  • 2
  • 11
11
votes
3 answers

python datetime gets object has no attribute today error

I have the following 2 different datetime uses: date=request.GET.get('date','') if date: date = datetime.strptime(date, "%m/%d/%Y") print date else: date = datetime.date.today() It seems the imports needed are: from…
Atma
  • 29,141
  • 56
  • 198
  • 299
11
votes
4 answers

Elegant way to convert python datetime.timedelta to dateutil.relativedelta

Is there an elegant was to convert between relativedelta and timedelta? The use case is getting user input ISO date. Python's isodate will return either isodate.duration.Duration or datetime.timedelta. We need the features of relativedelta (per What…
Williams
  • 4,044
  • 1
  • 37
  • 53