How to convert string to date in python? - python

reference_date is 02.03 but I would like to convert it to Month and Date (3rd of February) so as to export to Excel as type 'date'.
reference_date = tr.find('td').text
reference_date = '2023.' + reference_date
reference_date_str = reference_date
reference_date_obj = datetime.strptime(reference_date_str, '%y-%m-%d')

You need to make sure you pass a format that matches the structure of your string.
if the string is using dots, then the format structure should be similar.
This should do the trick:
reference_date = tr.find('td').text
reference_date_with_year = f"2023.{reference_date}"
reference_date_obj = datetime.strptime(reference_date_with_year, '%Y.%m.%d')
print(reference_date_obj)
output:
2023-02-03 00:00:00

reference_date = '2023.' + reference_date
reference_date_obj = datetime.strptime(reference_date_str, '%Y.%m.%d')
excel_date=reference_date_obj.strftime("%Y.%m.%d")
if you are using pandas to write to excel sheet, you can pass the date in any format as long as you specify the format in your stmt
pd.ExcelWriter("abc.xlsx",engine='xlsxwriter',date_format='YYYY.MM.DD')

Related

Is there a way to set weeknumber/year format from a datetime column in pandas

'''
'I'm trying to set a column in datetime format like this and create a new column Week_No in week number/year format but when I write it to excel it converts it to mm/year format'
datecolumn = ["Date"]
df_2021 = pd.read_csv("C:/Users/Lenovo/Downloads/2021.csv", sep = ";", parse_dates=datecolumn).iloc[:-7]
df_2022 = pd.read_csv("C:/Users/Lenovo/Downloads/2022.csv", sep = ";", parse_dates=datecolumn).iloc[:-7]
df_combined = pd.concat([df_2021,df_2022],ignore_index=False)
df_combined["Date_New"] = pd.to_datetime(df_combined["Date_New"], format='%d/%m/%Y' )
WeekNo = []
def getWeekYear(isodate):
year = isoDate[0]
week = isoDate[1]
# print(f"{week}-{year}")
cellValue = f"{week}-{year}"
# df_combined["Week_No"] = cellValue
# WeekNo = []
WeekNo.append(cellValue)
for value in df_combined["Date_New"]:
day = int(value[:2])
month = int(value[3:5])
year = int(value[6:])
isoDate = date(year, month, day).isocalendar()
getWeekYear(isoDate)
df_combined["Week_No"]= WeekNo
'''
'Also I need the Week_No to be in **Number ** format when I write it in excel'
If this only happens when you write it to excel it might be because the type of the excel cell is set to date, and by default, the content of the cell will be changed to the date format defined in excel.
I would suggest setting the type of the excel cells to text instead of date, so excel does not overwrite the content.
Otherwise, you can change the date format defined in your excel sheet, so the defined format is week number/year.

python error: can only concatenate str (not "datetime.timedelta") to str

i am trying to get the weeks between two dates and split into rows by week and here is the error message i got:
can only concatenate str (not "datetime.timedelta") to str
Can anyone help on this one? thanks!!!
import datetime
import pandas as pd
df=pd.read_csv(r'C:\Users\xx.csv')
print(df)
# Convert dtaframe to dates
df['Start Date'] = pd.to_datetime(df['start_date'])
df['End Date'] = pd.to_datetime(df['end_date'])
df_out = pd.DataFrame()
week = 7
# Iterate over dataframe rows
for index, row in df.iterrows():
date = row["start_date"]
date_end = row["end_date"]
dealtype = row["deal_type"]
ppg = row["PPG"]
# Get the weeks for the row
while date < date_end:
date_next = date + datetime.timedelta(week - 1)
df_out = df_out.append([[dealtype, ppg, date, date_next]])
date = date_next + datetime.timedelta(1)
# Remove extra index and assign columns as original dataframe
df_out = df_out.reset_index(drop=True)
df_out.columns = df.columns
df.to_csv(r'C:\Users\Output.csv', index=None)
date is a Timestamp object which is later converted to a datetime.timedelta object.
datetime.timedelta(week - 1) is a datetime.timedelta object.
Both of these objects can be converted to a string by using str().
If you want to concatenate the string, simply wrap it with str()
date_next = str(date) + str(datetime.timedelta(week - 1))
You converted the start_date and end_date column to datetime, but you added the converted columns as Start Date and End Date. Then, in the loop, you fetch row["start_date"], which is still a string. If you want to REPLACE the start_date column, then don't give it a new name. Spelling matters.

How can I can convert a timestamp column format from [YYYY/MM/DD] to from [DD/MM/YYYY] in Python data frame?

In a Python data frame, is there a way I can convert a timestamp column with objects formatted as [YYYY/MM/DD HH:MM:SS] to [DD/MM/YYYY HH:MM:SS] instead?
E.g. [2020/01/03 13:00:00] to [03/01/2020 13:00:00]
Where (YYYY is year), (MM is month), (DD is day), (HH is hour), (MM is mins), (SS is seconds).
Thank you in advance!
Here is an example of what you expect:
import datetime as dt
# Example of df with string for the date
df = pd.DataFrame({'date':['2020/01/03 13:00:00']})
# transforming it in a datetime with a specific format
df['date'] = pd.to_datetime(df.date,format='%Y/%m/%d %H:%M:%S')
#changing the format as you don't wish
df['colum1'] = df.date.dt.strftime('%Y/%m/%d %H:%M:%S')
#changing the format as you wish
df['colum2'] = df.date.dt.strftime('%d/%m/%Y %H:%M:%S')
df
OUTPUT :

getting the current date format of a parsed date object

Is there a way to get the format of a given string to be parsed with the datetime format?
Something like:
string_date= '2/1/99'
date_obj = parser.parse(string_date)
format1 = date_obj.getdateformat
format2 = anotherobj.getdateformat

Converting date and time into float in python

Below is my data set
Date Time
2015-05-13 23:53:00
I want to convert date and time into floats as separate columns in a python script.
The output should be like date as 20150513 and time as 235300
If all you need is to strip the hyphens and colons, str.replace() should do the job:
>>> s = '2015-05-13 23:53:00'
>>> s.replace('-', '').replace(':', '')
'20150513 235300'
For mort sophisticated reformatting, parse the input with time.strptime() and then reformat with time.strftime():
>>> import time
>>> t = time.strptime('2015-05-13 23:53:00', '%Y-%m-%d %H:%M:%S')
>>> time.strftime('%Y%m%d %H%M%S', t)
'20150513 235300'
If you have a datetime you can use strftime()
your_time.strftime('%Y%m%d.%H%M%S')
And if your variables are string, You can use replace()
dt = '2015-05-13 23:53:00'
date = dt.split()[0].replace('-','')
time = dt.split()[1].replace(':','')
fl = float(date+ '.' + time)
date = "2015-05-13".replace("-", "")
time = "10:58:56".replace(":", "")

Categories