I want to write a piece of code that checks if the given date value is the first minute of the month like x-x-01T00:00:00Z. I want to achieve the bool function that are given below. How would I be able to modify the code below so I am able to do that?
import datetime
import pandas as pd
import numpy
def dates(date_vals):
datetime = pd.to_datetime(date_vals)
for x in date_vals:
#Condition that l;ooss for if it is the first day and the minute of the month
#Is the dates format x-x-01T00:00:00Z
dates(np.array(['2015-10-08T13:40:00Z', '2016-01-04T21:00:00Z', '2015-10-01T00:00:00Z', '2015-10-08T00:00:00Z']))
Expected Output:
False
False
True
False
CodePudding user response:
This should work:
import datetime
import pandas as pd
import numpy as np
example = np.array(['2015-10-08T13:40:00Z', '2016-01-04T21:00:00Z', '2015-10-01T00:00:00Z', '2015-10-08T00:00:00Z'])
def my_dates_fun(date_vals):
datetime_conversion = pd.to_datetime(date_vals)
return (datetime_conversion.day == 1) & (datetime_conversion.hour == 0) & (datetime_conversion.minute == 0)
print(my_dates_fun(example))
Output:
[False False True False]
CodePudding user response:
Just set up a boolean mask:
dates = pd.to_datetime(['2015-10-08T13:40:00Z', '2016-01-04T21:00:00Z', '2015-10-01T00:00:00Z', '2015-10-08T00:00:00Z'])
mask = (dates.day == 1) & (dates.hour == 0) & (dates.minute == 0)
An even simpler mask is use strftime method to filter only day, hour and minute and look for 1st day, 0 hour, 0 minute datetimes:
mask = dates.strftime('%d %H:%M') == '01 00:00'
Output:
array([False, False, True, False])
CodePudding user response:
The best way is creating a panda interval with the first day of the month and first minute to the second minutes. Then checking the if the date is in the interval
You can achieve that with https://pandas.pydata.org/docs/reference/api/pandas.Interval.html
and creating new panda datetime using the .month / . year from the datetime passed in parameters.
CodePudding user response:
An alternative is to "floor" the dates:
dti = pd.to_datetime(np.array(['2015-10-08T13:40:00Z', '2016-01-04T21:00:00Z',
'2015-10-01T00:00:00Z', '2015-10-08T00:00:00Z']))
out = dti.tz_localize(None).to_period('M').to_timestamp() == dti.tz_localize(None)
print(out)
# Output
array([False, False, True, False])
CodePudding user response:
I'm assuming that you want False if a timestamp has a non-zero second/millisecond/... component, e.g. '2015-10-01T00:00:01Z' -> False.
>>> dates = pd.to_datetime(['2015-10-08T13:40:00Z', '2016-01-04T21:00:00Z', '2015-10-01T00:00:00Z', '2015-10-08T00:00:00Z', '2015-10-01T00:00:01Z'])
>>> dates.to_period('M').to_timestamp() == dates.tz_localize(None)
array([False, False, True, False, False])
Otherwise, use
>>> dates.to_period('M').to_timestamp() == dates.floor('min').tz_localize(None)
array([False, False, True, False, True]
