Get datetime format from string python

2024/10/10 10:22:47

In Python there are multiple DateTime parsers which can parse a date string automatically without providing the datetime format. My problem is that I don't need to cast the datetime, I only need the datetime format.

Example: From "2021-01-01", I want something like "%Y-%m-%d" or "yyyy-MM-dd".

My only idea was to try casting with different formats and get the successful one, but I don't want to list every possible format.

I'm working with pandas, so I can use methods that work either with series or the string DateTime parser.

Any ideas?

Answer

In pandas, this is achieved by pandas._libs.tslibs.parsing.guess_datetime_format

from pandas._libs.tslibs.parsing import guess_datetime_formatguess_datetime_format('2021-01-01')# '%Y-%m-%d'

As there will always be an ambiguity on the day/month, you can specify the dayfirst case:

guess_datetime_format('2021-01-01', dayfirst=True)
# '%Y-%d-%m'
https://en.xdnf.cn/q/69906.html

Related Q&A

Generating an optimal binary search tree (Cormen)

Im reading Cormen et al., Introduction to Algorithms (3rd ed.) (PDF), section 15.4 on optimal binary search trees, but am having some trouble implementing the pseudocode for the optimal_bst function in…

Pydub from_mp3 gives [Errno 2] No such file or directory

I find myself in front of a wall here, simply trying to load an audio file into pydub for converting it keeps on throwing a "[Errno 2] No such file or directory" error.Naturally I have spent …

Compile Python 3.6.2 on Debian Jessie segfaults on sharedmods

Im trying to compile Python 3.6.2 on a Debian Jessie box with the options./configure --prefix="/opt/python3" \ --enable-optimizations \--with-lto \ --enable-profiling \ --enable-unicode=ucs4 …

Escape space in filepath

Im trying to write a python tool that will read a logfile and process itOne thing it should do is use the paths listed in the logfile (its a logfile for a backup tool)/Volumes/Live_Jobs/Live_Jobs/*SCAN…

How to save web page as text file?

I would like to save a web page (all content) as a text file. (As if you did right click on webpage -> "Save Page As" -> "Save as text file" and not as html file)I have tried …

PIL Image.size returns the opposite width/height

Using PIL to determine width and height of imagesOn a specific image (luckily only this one - but it is troubling) the width/height returning from image.size is the opposite. The image: http://storage.…

Django Rest Framework: Register multiple serializers in ViewSet

Im trying to create a custom API (not using models), but its not showing the request definition in the schema (in consequence, not showing it in swagger). My current code is:views.pyclass InfoViewSet(v…

Which is most accurate way to distinguish one of 8 colors?

Imagine we how some basic colors:RED = Color ((196, 2, 51), "RED") ORANGE = Color ((255, 165, 0), "ORANGE") YELLOW = Color ((255, 205, 0), "YELLOW") GREEN = Color ((0, 128…

Lambda function behavior with and without keyword arguments

I am using lambda functions for GUI programming with tkinter. Recently I got stuck when implementing buttons that open files: self.file="" button = Button(conf_f, text="Tools opt.",…

How to get type annotation within python function scope?

For example: def test():a: intb: strprint(__annotations__) test()This function call raises a NameError: name __annotations__ is not defined error. What I want is to get the type annotation within the f…