Suppose I am given a partial datetime string. I wish to obtain the datetime it represents, and what was the resolution of the given datetime.
For example:
"2021-01-06 12"
->2021-01-06 12:00:00.000000
and"hour"
"2020-03"
->2020-03-01 00:00:00.000000
and"month"
"2020-03-01"
->2020-03-01 00:00:00.000000
and"day"
The idea is to use a given partial datetime-string as a time-range specification. Meaning that instead of writing "All of March, 2020"
, just write "2020-03"
.
The question can be reduced to an answer with the pandas
framework, though given a partial string like in the above examples, pd.Timestamp(...)
parses it well (e.g. pd.Timestamp("2020-03") == pd.Timestamp('2020-03-01 00:00:00.000000')
).
Thanks in advance!
EDIT: It seems that the internal function pandas._libs.tslibs.parsing.parse_datetime_string_with_reso
returns what I want. Does anyone know how can I access it (not accessible using from pandas._libs.tslibs.parsing import parse_datetime_string_with_reso
)?
CodePudding user response:
Addressing specifically this section of the question:
EDIT: It seems that the internal function pandas._libs.tslibs.parsing.parse_datetime_string_with_reso returns what I want. Does anyone know how can I access it (not accessible using from pandas._libs.tslibs.parsing import parse_datetime_string_with_reso)?
You can use from pandas._libs.tslibs.parsing import parse_time_string
which internally calls parse_datetime_string_with_reso
and also returns the resolution.
CodePudding user response:
dateutil
has a good parser that allows the input string to have missing parts:
from dateutil import parser
dates = ["2021-01-06 12", "2020-03", "2020-03-01"]
for date in dates:
if len(date.split('-')) <= 2:
# If day is missing, resulting day will be the same as
# the current day of month instead of '01'.
date = '-01'
parsed = parser.parse(date)
print(parsed)
output:
2021-01-06 12:00:00
2020-03-01 00:00:00
2020-03-01 00:00:00
CodePudding user response:
You can try this https://pypi.org/project/datefinder/ A python module for locating dates inside string.