python-datetime

Convert string into datetime.time object

孤街浪徒 提交于 2019-11-28 18:05:30
Given the string in this format "HH:MM" , for example "03:55" , that represents 3 hours and 55 minutes . I want to convert it to datetime.time object for easier manipulation. What would be the easiest way to do that? Use datetime.datetime.strptime() and call .time() on the result: >>> datetime.datetime.strptime('03:55', '%H:%M').time() datetime.time(3, 55) The first argument to .strptime() is the string to parse, the second is the expected format. >>> datetime.time(*map(int, '03:55'.split(':'))) datetime.time(3, 55) 来源: https://stackoverflow.com/questions/14295673/convert-string-into-datetime

Converting string date to timestamp in python 3.4

我只是一个虾纸丫 提交于 2019-11-28 14:31:50
I am trying to convert string date to timestamp in Python as described in the post here . When I run the code examples in the post, I encounter an error. For e.g.: >>> import time >>> import datetime >>> s = "01/12/2011" >>> time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple()) Traceback (most recent call last): File "<pyshell#31>", line 1, in <module> time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple()) File "C:\Python34\lib\_strptime.py", line 15, in <module> import calendar File "C:\Python34\calendar.py", line 9, in <module> calendar = wckCalendar.Calendar(root,

Pandas dataframe datetime to time then to seconds

谁说胖子不能爱 提交于 2019-11-28 13:01:28
I have a dataframe. A column contains timestamps. I would like to remove dates and convert the time to seconds. First I converted them to datetime: In: df_time = pd.to_datetime(df["Timestamp"]) Out: 0 2017-11-07 13:09:00 1 2017-11-07 13:11:00 2 2017-11-07 13:13:00 3 2017-11-07 13:15:00 dtype: datetime64[ns] Then I removed dates: In: df_time = pd.Series([val.time() for val in df_time]) Out: 0 13:09:00 1 13:11:00 2 13:13:00 3 13:15:00 4 13:17:00 dtype: object But they became objects and I did not managed to convert them to datetime-like objects to convert them into seconds. I know there are some

How do I determine if current time is within a specified range using Python's datetime module?

冷暖自知 提交于 2019-11-28 06:19:41
What would be the best way to see if the current time lies between say 10:30 AM and 4:30 PM . I could think of the following, not sure how correct: from datetime import datetime nw = datetime.now() hrs = nw.hour;mins = nw.minute;secs = nw.second; zero = timedelta(seconds = secs+mins*60+hrs*3600) st = nw - zero # this take me to 0 hours. time1 = st + timedelta(seconds=10*3600+30*60) # this gives 10:30 AM time2 = st + timedelta(seconds=16*3600+30*60) # this gives 4:30 PM if nw>= time1 or nw <= time2: print "yes, within the interval" Please let me know if this the correct approach, can something

Parse this date in Python: 5th November 2010

徘徊边缘 提交于 2019-11-28 02:03:28
问题 I'm having a bad time with date parsing and formatting today. Points for somebody who can parse this date format into a datetime.date or datetime.datetime (I'm not too fussy but I'd prefer .date ): 5th November 2010 回答1: Using dateutil: In [2]: import dateutil.parser as dparser In [3]: date = dparser.parse('5th November 2010') In [4]: date Out[4]: datetime.datetime(2010, 11, 5, 0, 0) 回答2: Unfortunately, strptime has no format characters for "skip an ordinal suffix" -- so, I'd do the skipping

Why is Python datetime time delta not found?

不羁的心 提交于 2019-11-27 23:39:51
问题 I am trying to make an array of dates in mmddyyyy format. The dates will start on the current day and then go two weeks into the future. So it all depends on the starting date. When I run my code I get an error that states: Traceback (most recent call last): File "timeTest.py", line 8, in <module> day = datetime.timedelta(days=i) AttributeError: type object 'datetime.datetime' has no attribute 'timedelta' I am not sure why this is happening because after searching online, I noticed that

Convert date format python

不问归期 提交于 2019-11-27 22:27:04
问题 I have django form and I am receiving from POST a date formated like "%d/%m/%Y" and I would like to convert it to "%Y-%m-%d", How could I do it? 回答1: Use strptime and strftime: In [1]: import datetime In [2]: datetime.datetime.strptime('10/05/2012', '%d/%m/%Y').strftime('%Y-%m-%d') Out[2]: '2012-05-10' Likewise, in Django template syntax you can use the date filter: {{ mydate|date:"Y-m-d" }} to print your date in your preferred format. 回答2: One way is to use strptime and strftime : >>> import

Python datetime strptime() and strftime(): how to preserve the timezone information

旧街凉风 提交于 2019-11-27 18:28:13
See the following code: import datetime import pytz fmt = '%Y-%m-%d %H:%M:%S %Z' d = datetime.datetime.now(pytz.timezone("America/New_York")) d_string = d.strftime(fmt) d2 = datetime.datetime.strptime(d_string, fmt) print d_string print d2.strftime(fmt) the output is 2013-02-07 17:42:31 EST 2013-02-07 17:42:31 The timezone information simply got lost in the translation. If I switch '%Z' to '%z', I get ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M:%S %z' I know I can use python-dateutil , but I just found it bizzare that I can't achieve this simple feature in datetime and have to

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

不羁岁月 提交于 2019-11-27 17:51:47
问题 I am accessing some data through an API where I need to provide the date range for my request, ex. start='20100101', end='20150415'. I thought I would speed this up by breaking up the date range into non-overlapping intervals and use multiprocessing on each interval. My problem is that how I am breaking up the date range is not consistently giving me the expected result. Here is what I have done: from datetime import date begin = '20100101' end = '20101231' Suppose we wanted to break this up

Why is pandas.to_datetime slow for non standard time format such as '2014/12/31'

坚强是说给别人听的谎言 提交于 2019-11-27 13:04:44
I have a .csv file in such format timestmp, p 2014/12/31 00:31:01:9200, 0.7 2014/12/31 00:31:12:1700, 1.9 ... and when read via pd.read_csv and convert the time str to datetime using pd.to_datetime , the performance drops dramatically. Here is a minimal example. import re import pandas as pd d = '2014-12-12 01:02:03.0030' c = re.sub('-', '/', d) %timeit pd.to_datetime(d) %timeit pd.to_datetime(c) %timeit pd.to_datetime(c, format="%Y/%m/%d %H:%M:%S.%f") and the performances are: 10000 loops, best of 3: 62.4 µs per loop 10000 loops, best of 3: 181 µs per loop 10000 loops, best of 3: 82.9 µs per