How to change a date to time from today in python? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a column in a pandas dataframe filled with dates (in the format YYYY-MM-DD).
I would like to write a code that would automatically change the values from time from today. That is if the date is 07/08/2019 it would automatically change that into the number one (because in one year), and if the date was in 6 months it would change into 0.5 (years). I need it to be precise to the day exactly as a fraction of the year, so like we could get number with a few decimal.
Would you kindly know how to do that ?

Firstly you need to parse the date string into a date object using strptime
Then get the difference between that and now. Convert it into days and divide by a year
Something like:
import datetime
then = datetime.datetime.strptime('2019-09-07', "%Y-%m-%d").date()
now = datetime.datetime.date().now()
print (then - now).days / 365.0

Related

is there a way that i can convert this dataframe into a datatype [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
already clean a lot of information into this, and i'm feel stuck now, if anyone can give some ideas and how to proceed i will apreacite so much.
later i need to join with this:
and timezone abbr. ¿there's a library or something that can handle datetypes like this?
my desired output is something like this: 2022-05-04 18:00:00 but i have this:
[]
As mentioned in the comment, you should make sure your dates are strings in order to concatenate them. You can do this like that:
df.column_name = df.column_name.astype(str)
After that you can use python's datetime library to format it as dates.
date = 'May-02-2022'
time = '9:00 AM'
timestamp = a + ' ' + b
timestamp = pd.to_datetime(timestamp, format='%b-%d-%Y %I:%M %p')
print(timstamp)
>>> Timestamp('2022-05-02 09:00:00')

How do I perform the below datetime operation? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a date with format in 'yyyy-dd-mm'. How do I convert to 'yyyy-mm-dd' format, been bit of a struggle with strftime and strptime!
Assuming the date is '1919-01-12', I want to see it in the format '1919-12-01'.
from datetime import datetime
date = '1919-01-12'
formatted_date = datetime.strptime(date, '%Y-%d-%m').strftime('%Y-%m-%d')
datetime.strptime(date, '%Y-%d-%m') creates a datetime object from a string, .strftime('%Y-%m-%d') formats the datetime as a string accoring to the specified pattern
try this code
import datetime
print (datetime.datetime.strptime("1919-01-12", "%Y-%d-%m").strftime("%Y-%m-%d"))

Create a time object? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a vast number of files that need to be deleted past a certain date.
I can't use the datestamp of the file because the files are created some time before they are used. A file called OCT21.txt needs to be deleted a few days after October 21st, but it could have been created in May.
My question is: is it possible to convert the "OCT21" string into a format that the time module can use?
This is in the datetime docs
classmethod datetime.strptime(date_string, format)
Return a datetime corresponding to date_string, parsed according to format.
This is equivalent to datetime(*(time.strptime(date_string, format)[0:6])).
ValueError is raised if the date_string and format can’t be parsed by time.strptime() or if it returns a value which isn’t a time tuple.
For a complete list of formatting directives, see strftime() and strptime() Behavior.
You can give it a format (%b%d in your case) and it'll return the datetime.
Try this:
from datetime import datetime
target = datetime.now().strftime('%b%d').upper()
if filename == target + '.txt':
# Do your thing
It is possible, have a look at time.strptime.
import time
filedate = time.strptime("OCT21", "%b%d")
Of course, in the resulting struct_time neither the year nor exact time are valid.

How to convert MS Excel numbers in general format (e.g 43422) to date in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
After fetching data from Excel to Python, the dates have been fetched into python as '43422' format. How do I convert this integer to date format in python now?
You may be able to prevent this from happening in the first place when you read in the data: pd.read_excel('file.xlsx'). Make sure that the data is properly formatted as a date before you read in the file.
If you want to convert this number to a date after you read it in, use the fact that it represents the number of days since January 1, 1900:
import datetime
d = '45422'
date = datetime.date(1900, 1, 1) + datetime.timedelta(int(d))
date
Output:
datetime.date(2024, 5, 12)

Extracting time data from odd Dd HH:MM:SS.mm format [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to import experimental time data which is not really a time, date, or datetime according to the python formalism. It is a time-elapsed format as Dd HH:MM:SS.m where the first time point would be 0d 00:00:00.00 and a point 26 hours later would be 1d 02:00:00.00.
Is there a way to use the datetime module to extract the appropriate time information without hard-coding a string search for the day and adding a multiple of 24 to the hour counter?
To convert your 'odd' format to datetime.timedelta object try:
from datetime import timedelta
import re
input = '1d 02:00:00.00'
def to_timedelta(input):
reg = re.search('([0-9])+d ([0-9]){2}:([0-9]){2}:([0-9]){2}\.([0-9]){2}', input)
ints = tuple(int(t) for t in reg.groups())
return timedelta(days=ints[0],
hours=ints[1],
minutes=ints[2],
seconds=ints[3],
milliseconds=ints[4])
td = to_timedelta(input)
print(td)

Categories

Resources