Bonjour Cher tous.

j'ai un Dataframe avec une colonne date dont le type est object.

elle est au format 22/03/25 ( jj/mm/aa)

je veux la convertir en Type date.

mais je rencontre une erreur ValueError

Voici le code que j'ai utilisé

Code : Sélectionner tout - Visualiser dans une fenêtre à part
df_copy['Date'] = pd.to_datetime(df_copy['Date'], format='%Y-%m-%d')
cela me renvoi l'erreur suvante.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
ValueError                                Traceback (most recent call last)
Cell In[10], line 1
----> 1 df_copy['Date'] = pd.to_datetime(df_copy['Date'], format='%Y-%m-%d')
      2 df_copy['N° Pièce'] = df_copy['N° Pièce'].astype('str')
      3 df_copy['Total HT'] = df_copy['Total HT'].astype('int64')
 
File D:\Projets Pycharm\.venv\Lib\site-packages\pandas\core\tools\datetimes.py:1068, in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
   1066             result = arg.tz_localize("utc")
   1067 elif isinstance(arg, ABCSeries):
-> 1068     cache_array = _maybe_cache(arg, format, cache, convert_listlike)
   1069     if not cache_array.empty:
   1070         result = arg.map(cache_array)
 
File D:\Projets Pycharm\.venv\Lib\site-packages\pandas\core\tools\datetimes.py:249, in _maybe_cache(arg, format, cache, convert_listlike)
    247 unique_dates = unique(arg)
    248 if len(unique_dates) < len(arg):
--> 249     cache_dates = convert_listlike(unique_dates, format)
    250     # GH#45319
    251     try:
 
File D:\Projets Pycharm\.venv\Lib\site-packages\pandas\core\tools\datetimes.py:435, in _convert_listlike_datetimes(arg, format, name, utc, unit, errors, dayfirst, yearfirst, exact)
    433 # `format` could be inferred, or user didn't ask for mixed-format parsing.
    434 if format is not None and format != "mixed":
--> 435     return _array_strptime_with_fallback(arg, name, utc, format, exact, errors)
    437 result, tz_parsed = objects_to_datetime64(
    438     arg,
439     dayfirst=dayfirst,
   (...)    443     allow_object=True,
    444 )
    446 if tz_parsed is not None:
    447     # We can take a shortcut since the datetime64 numpy array
    448     # is in UTC
 
File D:\Projets Pycharm\.venv\Lib\site-packages\pandas\core\tools\datetimes.py:469, in _array_strptime_with_fallback(arg, name, utc, fmt, exact, errors)
    458 def _array_strptime_with_fallback(
    459     arg,
    460     name,
   (...)    464     errors: str,
    465 ) -> Index:
    466     """
    467     Call array_strptime, with fallback behavior depending on 'errors'.
    468     """
--> 469     result, tz_out = array_strptime(arg, fmt, exact=exact, errors=errors, utc=utc)
    470     if tz_out is not None:
    471         unit = np.datetime_data(result.dtype)[0]
 
File pandas/_libs/tslibs/strptime.pyx:501, in pandas._libs.tslibs.strptime.array_strptime()
 
File pandas/_libs/tslibs/strptime.pyx:451, in pandas._libs.tslibs.strptime.array_strptime()
 
File pandas/_libs/tslibs/strptime.pyx:583, in pandas._libs.tslibs.strptime._parse_with_format()
 
ValueError: time data "21/01/25" doesn't match format "%Y-%m-%d", at position 0. You might want to try:
    - passing `format` if your strings have a consistent format;
    - passing `format='ISO8601'` if your strings are all ISO8601 but not necessarily in exactly the same format;
    - passing `format='mixed'`, and the format will be inferred for each element individually. You might want to use `dayfirst` alongside this.

Quelqu'un pourrait m'aider ?

cordialement