Find max date less than today - python

I’m trying to locate the current date (in this example the date was 1/15/2021) across all columns and return the result in the “current status” column.
The logic behind this in excel is max(if(B2:E2<=“1/15/2021”,B2:E2))
I’m not sure how to work this out through python
Thanks in advance

I would just do this
from datetime import datetime as dt
df['current status'] = dt.now().date()
this would be the easiest way

Related

How to include a dynamic date in a string

I'm trying to write a daily csv file but would like the title of the csv file to specify today's date.
It seems to be failing every time but I'm sure there's an easy way to do this..?
Hopefully this isn't a duplicate but can't seem to find another question similar.
At the minute I've just tried this;
from datetime import date
morningupdate.to_csv('morningupdate' + '_' + date.today() '.csv')
My brain is completely broken with this, any help much appreciated!
Does this solve your problem?
from datetime import date
morningupdate.to_csv(f'morningupdate_{date.today()}.csv')
You are trying to concatenate string with a datetime object.
You can use f string to manage the problem:
from datetime import date
path = f'morningupdate_{date.today()}.csv'
morningupdate.to_csv(path)

Python xlwings - TypeError: must be real number, not Timedelta

I have a pandas dataframe containing columns in timedelta64[ns] format.
For this project I cannot use df.to_excel() and I need to import the dataframe via xlwings so that it prints into existing Excel Workbook and keeps its format.
When I try the usual:
workbook_sablona_dochazka.sheets[zamestnanec].range('A1').options(index=False).value = individual_dochazka_zamestnanec
I receive error:
TypeError: must be real number, not Timedelta
Is there a way to format my timedelta64[ns] so that xlwings would be able to import the dataframe in? I need to preserve my time values so that it becomes 12:30:00 in Excel again after xlwings import and maybe some after-formatting inside the Excel itself.
I tried:
individual_dochazka_zamestnanec['Příchod do práce'] = individual_dochazka_zamestnanec['Příchod do práce'].values.astype(float)
This worked around the error but imported columns had totally out of sense numbers.
Any idea how to work around this?
Thank you very much in advance!
If the error says it's a "TimeDelta", then you have to ask delta relative to what? Usually a TimeDelta indicates something like "three hours" or "minus two days". You say you'd like the output to be "12:30:00" is that an actual time or does it mean 12 hours 30 minutes and no seconds?
You could try making the TimeDelta relative to "the beginning of time" so that it's a date which can be imported into xlwings but is formatted like a time as suggested here.
Just managed to figure it out.
The trick was to first format timedelta64[ns] columns as string and then trim it a bit with .map(lambda x:str(x)[7:])) so that I would get that nice time only stamp.
individual_dochazka_zamestnanec['Počet odpracovaných hodin celkem'] = ((individual_dochazka_zamestnanec['Počet odpracovaných hodin celkem']).astype(str)).map(lambda x: str(x)[7:])
To my surprise, Excel accepted this without issue which is exactly what I needed.
Hope this helps someone, sometime.
Cheers!

Convert the day time on python

I have a question based on the conversion of a date and time using the start_time element's Google Calendar API, how could I convert it to a day, month and year of Latin America without more, if you could help me please
the datetime:
2021-01-19T09:00:00-05:00
I need the follow example: day/mount/year 15/01/2021
I've been trying and can't find a way to convert properly, please if you could help me.
Use the datetime library:
import datetime
date_str = "2021-01-19T09:00:00-05:00"
date_obj = datetime.datetime.strptime(date_str[:10], "%Y-%m-%d").strftime("%d/%m/%Y")
print(date_obj)

Finding the Max() Datetime Pandas Python

I have a question about using dates on pandas.
In the CSV I am importing (if I ordering it), I will find that the maximum date is 10/09/2019 18:22:00
Immediately after importing (still as object), the date that appears is 31/12/2018 12:05.
And if I convert in this way to date and time:
df['Data_Abertura_Processo'] = pd.to_datetime(df['Data_Abertura_Processo'])
the value changes to: Timestamp('2019-12-08 18:40:00').
How do I get the maximum date I find into the CSV by filtering in Excel itself?
Today I'm using:
df['Data_Abertura_Processo'].max()
Am I wrong in converting or using max ()?
df['Data_Abertura_Processo'] = pd.to_datetime(df['Data_Abertura_Processo'],format="%d/%m/%Y %H:%M:%S")
Make sure that your datetimes have all the same format.

Is there a replacement for this syntax?

with the new update on pandas I can't use this function that I used on on Datacamp learning course - (DAYOFWEEK doesn't exist anymore)
days_of_week = pd.get_dummies(dataframe.index.dayofweek,
prefix='weekday',
drop_first=True)
How can I change the syntax of my 'formula' to get the same results?
Sorry about the silly question but spent a lot of time here and I'm stuck...
Thanks in advance!
already tried just using the dataframe with index but doesn't get the days of the week on the get dummies\
used datetimeindex but messing up on the formulation as well
`days_of_week = pd.get_dummies(dataframe.index.dayofweek, prefix='weekday', drop_first=True)`
the dataframe is fairly big and need the outputs to get me the weekdays because I'm dealing with stock prices
Try weekday instead of dayofweek.
So
days_of_week = pd.get_dummies(dataframe.index.weekday,
prefix='weekday',
drop_first=True)
See docs below:
pandas.Series.dt.weekday

Categories

Resources