how to drop rows having a specific time in datatime index - pandas

How can I remove the whole row from the dataframe where the datetime column is having 07:15 time in the datetime column.

Suppose your first column is named 'Datetime' and has a datetime64 dtype, use:
import datetime
# Not mandatory if Datetime is already datetime64
# df['Datetime'] = pd.to_datetime(df['Datetime'])
out = df[df['Datetime'].dt.time != time(7, 15)]

Related

Convert to DateTime datatype in Pandas

How do I convert a pandas column to datetime format?
I have tried this and it did not work.
odu1['DateTime'] = pd.to_datetime(odu1['DateTime'].str.strip(), format='%m/%d/%Y %I:%M:%S %p')
Here is one example entry of how values in my column look like 06/01/2022 12:00:06 AM

Pandas - Converting datetime field to a specified format

I am trying to get a date time field in Pandas in the below format
df['date'] = pd.to_datetime(df['date'])
The above code returns date time column in the below format
2021-11-27 03:30:00
I would like to get an output of 27/11/2021 (format is dd/mm/yyyy) and the data type of the column needs to be datetime and not object.
If your column is a string, you will need to first use pd.to_datetime,
df['Date'] = pd.to_datetime(df['Date'])
Then, use .dt datetime accessor with strftime:
df = pd.DataFrame({'Date':pd.date_range('2017-01-01', periods = 60, freq='D')})
df.Date.dt.strftime('%Y%m%d').astype(int)
Or use lambda function:
df.Date.apply(lambda x: x.strftime('%Y%m%d')).astype(int)

Convert TZ datetime to timestamp

I have column of dates in the format below in a pandas dataframe.
What is the most effective way to convert
2021-11-06T21:54:35.825Z
to
2021-11-6 21:54:35
pd.to_datetime(df['date'], format='%Y-%m-%d %H:%M:%S') only returns 2021-11-06 without the timestamp
You can use .dt accessor on Pandas Series followed by by .strftime property dt.strftime, to format datetime into desired string representation.
import pandas as pd
import datetime
df = pd.DataFrame({'date': ["2021-11-06T21:54:35.825Z"]})
fmt = '%Y-%m-%d %H:%M:%S'
pd.to_datetime(df['date']).dt.strftime(fmt)
returns
0 2021-11-06 21:54:35
Name: date, dtype: object
Or if you don't want to have zero padding before the day, you can use: fmt="%Y-%m-%-d %H:%M:%S" (notice the hyphen between % and d). This results in: 2021-11-6 21:54:35

Comparision between date and integer in pandas

I have dataset df with date column. I have dates from 2020-01-01 to 2021-03-30 in date column. Now i have a variable like a=20210130(which is actually a date). I need take take values from the df which is <=a.
First idea is convert a to datetimes and compare, then filter by boolean indexing:
df['date'] = pd.to_datetime(df['date'])
a = 20210130
df = df[df['date'] <= pd.to_datetime(a)]
Or convert column to integers and compare:
a = 20210130
df = df[df['date'].dt.strftime('%Y%m%d').astype(int) <= a]

column with dates into datetime index in dask

pd.DatetimeIndex(df_dask_dataframe['name_col'])
I have a dask dataframe for which I want to convert a column with dates into datetime index. However I get a not implemented error. Is there a workaround?
I think you need dask.dataframe.DataFrame.set_index if dtype of column is datetime64:
df_dask_dataframe = df_dask_dataframe.set_index('name_col')