How would I go about designing a Python program which takes in a date from the user, a date that looks like this 3/13/17, and turns it into a date which looks like this 2017.3.13?
You can split the string by using the str.split method like this:
s = "3/13/17"
month, day, year = s.split("/")
print(f"20{year}.{month}.{day}")
Python will automatically assign the splitted values to the variables month, day, and year
Get the date as text and then convert it to date with the format you would like. or get the date as a number (month, date and year ) separately and make it as a date.
Example:
my_string = str(input('Enter date(yyyy-mm-dd): '))
my_date = datetime.strptime(my_string, "%Y-%m-%d")
I am trying to convert the date of birth in pandas from mm/dd/yy to mm/dd/yyyy. See screenshot below:
The issue im having is when converting date of birth from
06/13/54
04/15/70
to the mm/dd/yyyy format it is assuming that the date is in the 2000's. Obviously those users wouldn't even be born yet. Is there a function or something that can be used to make sure the conversion is done properly or as proper as it can be. Let's assume for this case no user lives past 90.
You really shouldn't strftime back to the very bad, not good mm-dd-yy format, but keep things as Pandas datetimes.
Either way, you can come up with a function that fixes "bad-looking" dates and .apply() it – this is using a single pd.Series, but that's what dataframes are composed of anyway, so you get the idea.
>>> s = pd.Series(["06/13/54", "04/15/70"])
>>> s2 = pd.to_datetime(s)
0 2054-06-13
1 2070-04-15
dtype: datetime64[ns]
>>> def fix_date(dt):
... if dt.year >= 2021: # change threshold accordingly
... return dt.replace(year=dt.year - 100)
... return dt
...
>>> s3 = s2.apply(fix_date)
0 1954-06-13
1 1970-04-15
dtype: datetime64[ns]
>>>
Simply replace the year if it is in the future?
x = pd.to_datetime('06/13/54',format='%m/%d/%y')
if x>datetime.datetime.now():
x.replace(year=x.year-100)
Input
df = pd.DataFrame({'date':["06/13/54", "04/15/70"]})
df.date = pd.to_datetime(df.date, format='%m/%d/%y')
df
Input df
date
0 2054-06-13
1 1970-04-15
Code
df.date = df.date.mask(df.date.gt(pd.Timestamp('today')), df.date-pd.DateOffset(years=100))
df
Output
date
0 1954-06-13
1 1970-04-15
I am trying to format the due date column of my dataframe from strings to dates from the datetime class. It seems to work within my for-loop, however, when I leave the loop, the values in my dataframe do not change.
df.replace() is obsolete, and .iat and .at do not work either. Code below. Thanks!
for x in df['due date']:
if type(x) == str:
date = x.split('/')
month = int(date[0])
day = int(date[1])
year = int(date[2].split(' ')[0])
formattedDate = dt.date(year, month, day)
df.at[x, "due date"] = formattedDate
Unless I'm missing something here, you can just pass the column to the built in 'to_datetime' function.
df['due date'] = pd.to_datetime(df['due date'],format="%m/%d/%Y")
That is assuming your date format is something like: 02/24/2021
If you need to change the date format, see the following:
strftime and strptime behavior
I have a small question. I have an array that saves dates in the following format.
'01/02/20|07/02/20'
It is saved as a string, which uses the start date on the left side of the "|" and end date on the other side.
It is only the end date that matters here, but is there a function or algorithm I can use to automatically calculate the difference in days and months between now.datetime and the end date (right-hand side of "|")?
Thanks, everyone
datetime.strptime is the main routine for parsing strings into datetimes. It can handle all sorts of formats, with the format determined by a format string you give it:
In [34]: from datetime import datetime
In [35]: end_date = datetime.strptime(s.split('|')[1], '%d/%m/%y')
In [36]: diff = datetime.now() - end_date
In [37]: diff
Out[37]: datetime.timedelta(days=81, seconds=81712, microseconds=14069)
In [38]: diff.days
Out[38]: 81
You might be looking for something like this.
Python datetime module is the way to go for a problem like this
import datetime
dates = '01/02/20|07/02/20'
enddate = dates.split('|')[-1]
# Use %y for 2 digits year else %Y for 4 digit year
enddate = datetime.datetime.strptime(enddate, "%d/%m/%y")
today = datetime.date.today()
print(abs(enddate.date() - today).days)
Output:
81
I have a .txt file data-set like this with the date column of interest:
1181206,3560076,2,01/03/2010,46,45,M,F
2754630,2831844,1,03/03/2010,56,50,M,F
3701022,3536017,1,04/03/2010,40,38,M,F
3786132,3776706,2,22/03/2010,54,48,M,F
1430789,3723506,1,04/05/2010,55,43,F,M
2824581,3091019,2,23/06/2010,59,58,M,F
4797641,4766769,1,04/08/2010,53,49,M,F
I want to work out the number of days between each date and 01/03/2010 and replace the date with the days offset {0, 2, 3, 21...} yielding an output like this:
1181206,3560076,2,0,46,45,M,F
2754630,2831844,1,2,56,50,M,F
3701022,3536017,1,3,40,38,M,F
3786132,3776706,2,21,54,48,M,F
1430789,3723506,1,64,55,43,F,M
2824581,3091019,2,114,59,58,M,F
4797641,4766769,1,156,53,49,M,F
I've been trying for ages and its getting really frustrating. I've tried converting to datetime using the datetime.datetime.strptime( '01/03/2010', "%d/%m/%Y").date() method and then subtracting the two dates but it gives me an output of e.g. '3 days, 0:00:00' but I can't seem to get an output of only the number!
The difference between two dates is a timedelta. Any timedelta instance has days attribute that is an integer value you want.
This is fairly simple. Using the code you gave:
date1 = datetime.datetime.strptime('01/03/2010', '%d/%m/%Y').date()
date2 = datetime.datetime.strptime('04/03/2010', '%d/%m/%Y').date()
You get two datetime objects.
(date2-date1)
will give you the time delta. The mistake you're making is to convert that timedelta to a string. timedelta objects have a days attribute. Therefore, you can get the number of days using it:
(date2-date1).days
This generates the desired output.
Using your input (a bit verbose...)
#!/usr/bin/env python
import datetime
with open('input') as fd:
d_first = datetime.date(2010, 03, 01)
for line in fd:
date=line.split(',')[3]
day, month, year= date.split(r'/')
d = datetime.date(int(year), int(month), int(day))
diff=d - d_first
print diff.days
Gives
0
2
3
21
64
114
156
Have a look at pleac, a lot of date-example there using python.