I have a massive spreadsheet in which all dates are written this way:
2 days ago
9 days ago
34 days ago
54 days ago
etc.
Is there a clever Python way to convert these data to the actual dates, if I tell Python what date '1 day ago' is?
Use timedelta.
Extract the value from that string in your spreadsheet and then use
d = date.today() - timedelta(days_to_subtract)
If the input date format may slightly vary (human input) then you could use parsedatetime module to parse human-readable date/time text into datetime objects:
#!/usr/bin/env python
import sys
from datetime import datetime
import parsedatetime # $ pip install parsedatetime
now = datetime(2015, 3, 8) # the reference date
cal = parsedatetime.Calendar()
for line in sys.stdin: # at most one date per line
dt, type = cal.parseDT(line, now)
if type > 0:
print(dt)
Output
2015-03-06 00:00:00
2015-02-27 00:00:00
2015-02-02 00:00:00
2015-01-13 00:00:00
Related
Portal epochconverter.com converts timestamp 1531423084013 to correct date of Thursday, July 12, 2018 3:18:04.013 PM GMT-04:00 DST. But in Python 2.7.12 I got below which is wrong
>>> timestamp=1531423084013
>>> time.ctime(timestamp).rsplit(' ', 1)[0]
'Wed Nov 12 00:06:53'
How to make it correct ?
1531423084013 is in milliseconds not is seconds.
As you can see from epochconverter.com the hour is : 3:18:04.013, so the seconds part is 4.013, this site handle time in seconds and in milliseconds (it seems when the input has 13 digits instead of 10 for time around nowadays).
But time.ctime() from python handle only time in seconds and this is why you get a wrong answer when you enter a time in milliseconds (in my system it throws an out of range).
So you must divide your time in milliseconds by 1000 :
time.ctime(1531423084)
'Thu Jul 12 21:18:04 2018'
(My time zone is UTC+0200)
I'm trying to take a date, for example Aug 22, 2017 02:00 PM EDT
and get the month, day, year from it.
month = re.findall(r'', date)[0]
day = re.findall(r'', date)[0]
year = re.findall(r'', date)[0]
I've started with something like this:
(.*)(?<=[a-zA-Z]{3}\s)
for the month. Is there a better way to do this?
You need to first convert to datetime and then extract the needed values like this (reusing the example):
from datetime import datetime
datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
print(datetime_object.year, datetime_object.month, datetime_object.day)
From what I can see you probably won't need to specify the format but pass the string directly to the datetime.strptime function.
I have a dataframe as following, the index is datetime(every Friday in a week).
begin close
date
2014-1-10 1.0 2.5
2014-1-17 2.6 2.6
........................
2016-12-30 3.5 3.8
2017-6-16 4.5 4.7
I want to extract the previour 2 year data from 2017-6-16. My code is following.
import datetime
from dateutil.relativedelta import relativedelta
df_index = df.index
df_index_test = df_index[-1] - relativedelta(years=2)
df_test = df[df_index_test:-1]
But it seems it is wrong, since the day of df_index_test may not in the dataframe.
Thanks!
You need boolean indexing, instead relativedelta is possible use DateOffset:
df_test = df[df.index >= df_index_test]
Sample:
rng = pd.date_range('2001-04-03', periods=10, freq='15M')
df = pd.DataFrame({'a': range(10)}, index=rng)
print (df)
a
2001-04-30 0
2002-07-31 1
2003-10-31 2
2005-01-31 3
2006-04-30 4
2007-07-31 5
2008-10-31 6
2010-01-31 7
2011-04-30 8
2012-07-31 9
df_test = df[df.index >= df.index[-1] - pd.offsets.DateOffset(years=2)]
print (df_test)
a
2011-04-30 8
2012-07-31 9
My input is input_time = "May 5 2016 11:29:32".
Expected output should be in seconds or milli seconds which is type of integer i.e., output_time = 2424241313113.
The above conversion should be done in python. How to do this conversion?
Here's how to convert date time into epoch seconds (dated starting from 00:00:00 UTC on 1 January 1970)
In Python 3.3+
from datetime import datetime
datetime.strptime('May 5 2016 11:29:32','%b %d %Y %H:%M:%S').timestamp()
In Python 2.7.9
datetime.strptime('May 5 2016 11:29:32','%b %d %Y %H:%M:%S').strftime('%s')
Note that strftime('%s') use your local time zone.
I want to generate the current day number using RubyMotion code. I have looked at several IOS solutions but I'm not experienced enough to translate the code successfully to RubyMotion.
I am currently at the following point:
def today
NSDate.today
end
def day_number
NSDate.from_components (day: today)
end
When I run the above it gives me an return of 3852055-06-16 00:00:00 +0100. I thought that the 3852055 part was seconds but it doesn't seem to equate to either todays date or to 16th of June - and in any case why should it be returning 06-16 instead of 02-08?? Totally confused here.
I just want to get todays day number. As I write the date is 2nd August 2014 and the day number should be 214 so I'm obviously way out somewhere.
Any help would be greatly appreciated.
cheers
This will do the trick:
daynum = NSCalendar.currentCalendar.ordinalityOfUnit(NSDayCalendarUnit, inUnit:NSYearCalendarUnit, forDate:NSDate.date)
Now suppose you have the date in the form of a string, and you want to get the day number for it:
datestr = "2014-01-01 11:08:56 +0000"
First create an NSDateFormatter to convert the String into an NSDate
df = NSDateFormatter.new
df.dateFormat = "yyyy-MM-dd HH:mm:ss z"
mydate = df.dateFromString(datestr)
daynum = NSCalendar.currentCalendar.ordinalityOfUnit(NSDayCalendarUnit, inUnit:NSYearCalendarUnit, forDate:mydate)
If your date string is simpler:
datestr = "2014-01-01"
just use a simpler dateFormat string:
df.dateFormat = "yyyy-MM-dd"
I suggest you to take a look at motion-support gem and especially at core-exttime.
You can play with dates as you want:
=> Mon, 04 Aug 2014
(main)> Time
=> Time
(main)> Time.today
=> 2014-08-04 00:00:00 +0200
(main)> Date.today.day
=> 4
(main)> Time.today.day
=> 4
and a lot more.