În acest articol, vom vedea cum să convertim DateTime în data în Python. Pentru aceasta, vom folosi metoda strptime() și modulul Pandas. Această metodă este folosită pentru a crea un obiect DateTime dintr-un șir. Apoi vom extrage data din obiectul DateTime folosind funcția date(). și dt.data de la Pandas în Python.
Metoda 1: Convertiți DateTime în dată în Python folosind DateTime
Clasele de lucru cu data și ora sunt oferite de către Python Datetime modul. Aceste clase oferă numeroase capacități de a se ocupa de date, ore și intervale de timp. Python tratează data și DateTime ca obiecte, așa că atunci când lucrați cu ele, lucrați cu obiecte mai degrabă decât cu șiruri de caractere sau marcaje temporale.
Sintaxa lui strptime()
Sintaxă: datetime.strptime()
Parametri:
clasa abstractă javaarg: Poate fi integer, float, tuple, Series, Dataframe pentru a fi convertit în datetime ca format de tip de date: Acesta va fi str, dar implicit este None. Timpul strf pentru a analiza timpul, de exemplu %d/%m/%Y, rețineți că %f va analiza până la nanosecunde.
Exemplul 1: Convertiți DateTime în dată
În acest exemplu, am creat un datetime_str care este 24AUG2001101010 , iar formatul său este %d%b%Y%H%M%S.
Python3
# import important module> import> datetime> from> datetime>import> datetime> # Create datetime string> datetime_str>=> '24AUG2001101010'> print>(>'datetime string : {}'>.>format>(datetime_str))> # call datetime.strptime to convert> # it into datetime datatype> datetime_obj>=> datetime.strptime(datetime_str,> >'%d%b%Y%H%M%S'>)> # It will print the datetime object> print>(datetime_obj)> # extract the time from datetime_obj> date>=> datetime_obj.date()> print>(date)> |
>
>
Ieșire:
datetime string : 24AUG2001101010 2001-08-24 10:10:10 2001-08-24>
Exemplul 2: Convertiți DateTime cu o dată numerică.
În acest exemplu, am creat un datetime_str care este 100201095407 , iar formatul său este %d%m%y%H%M%S .
Python3
ciclul de viață sdlc
# import important module> import> datetime> from> datetime>import> datetime> # Create datetime string> datetime_str>=> '100201095407'> print>(>'datetime string : {}'>.>format>(datetime_str))> # call datetime.strptime to convert> # it into datetime datatype> datetime_obj>=> datetime.strptime(datetime_str,> >'%d%m%y%H%M%S'>)> # It will print the datetime object> print>(datetime_obj)> # extract the time from datetime_obj> date>=> datetime_obj.date()> # it will print date that we have> # extracted from datetime obj> print>(date)> |
>
>
Ieșire :
datetime string : 100201095407 2001-02-10 09:54:07 2001-02-10>
Exemplul 3: Convertiți DateTime cu data curentă.
În acest exemplu, luăm data și ora actuală și extragem data acesteia din obiect.
Python3
# import important module> from> datetime>import> datetime> # call datetime.strptime to> # convert it into datetime datatype> datetime_obj>=> datetime.now()> # It will print the datetime object> print>(datetime_obj)> # extract the time from datetime_obj> date>=> datetime_obj.date()> print>(date)> |
>
>
Ieșire:
2021-08-07 06:30:20.227879 2021-08-07>
Metoda 2: convertiți DateTime în data în Python folosind Pandas
Pandas oferă un set diferit de instrumente cu ajutorul cărora putem efectua toate sarcinile necesare privind datele dată-oră. Să încercăm să înțelegem cu exemplele discutate mai jos.
Exemplu:
Valoarea datei și valoarea DateTime sunt ambele afișate în ieșire folosind comanda print. Valorile DateTime sunt adăugate mai întâi la o coloană a unui Pandas DataFrame. Valoarea DateTime este apoi convertită într-o valoare de dată utilizând funcția dt.date().
Python3
import> pandas as pd> df>=> pd.DataFrame({>'time'>: [>'2022-7-16 11:05:00'>,> >'2025-7-18 12:00:30'>]})> print>(df)> df[>'time'>]>=> pd.to_datetime(df[>'time'>]).dt.date> print>(df)> |
>
>
funcții în c
Ieșire:
time 0 2022-7-16 11:05:00 1 2025-7-18 12:00:30 time 0 2022-07-16 1 2025-07-18>