admin管理员组

文章数量:1125959

I'm having difficulty with Pandas when trying to convert this column to a date. The table doesn't include a year, so I think that's making the conversion difficult.

    28 JUL  Unnamed: 0        Alura *Alura - 7/12     68,00
0   28 JUL         NaN  Passei Direto S/A. - 3/12     19,90
1   31 JUL         NaN          Drogarias Pacheco     25,99
2   31 JUL         NaN     Mundo Verde - Rj - Sho      5,90
3   31 JUL         NaN              Paypal *99app      4,25
4   04 AGO         NaN            Saldo em atraso  1.091,17
5   04 AGO         NaN          Crédito de atraso  1.091,17
6   06 AGO         NaN             Apple.Com/Bill     34,90
7   07 AGO         NaN        Pagamento em 07 AGO  1.091,17
8   07 AGO         NaN            Juros de atraso     16,86
9   07 AGO         NaN              IOF de atraso      4,43
10  07 AGO         NaN            Multa de atraso     21,91
11  08 AGO         NaN             Apple.Com/Bill     21,90
12  09 AGO         NaN      Google Youtubepremium     20,90
13  10 AGO         NaN              Amazon.Com.Br     41,32
14  12 AGO         NaN           Uber *Uber *Trip     17,91
15  12 AGO         NaN           Uber *Uber *Trip     16,94
16  12 AGO         NaN                Mia Cookies     47,50
17  13 AGO         NaN           Uber *Uber *Trip     16,96
18  13 AGO         NaN           Uber *Uber *Trip     19,98
19  16 AGO         NaN           Uber *Uber *Trip     11,93
20  16 AGO         NaN           Uber *Uber *Trip      9,97
21  18 AGO         NaN           Uber *Uber *Trip      9,91
22  22 AGO         NaN           Uber *Uber *Trip      9,96
23  23 AGO         NaN              Amazonprimebr     14,90
24  27 AGO         NaN        Paypal *Sacocheiotv     15,00
25  27 AGO         NaN        Pag*Easymarketpleno      6,50

I tried to transform it using this code, but it's not working:

df["Data"] = pd.to_datetime(df["Data"], format="%d %b", errors="coerce")

Incorrect output:

Data                      Local  Valor
0  1900-07-28        Alura *Alura - 7/12  68,00
1  1900-07-28  Passei Direto S/A. - 3/12  19,90
2  1900-07-31          Drogarias Pacheco  25,99
3  1900-07-31     Mundo Verde - Rj - Sho   5,90
4  1900-07-31              Paypal *99app   4,25
7         NaT             Apple.Com/Bill  34,90
9         NaT            Juros de atraso  16,86
10        NaT              IOF de atraso   4,43
11        NaT            Multa de atraso  21,91
12        NaT             Apple.Com/Bill  21,90
13        NaT      Google Youtubepremium  20,90
14        NaT              Amazon.Com.Br  41,32
15        NaT           Uber *Uber *Trip  17,91
16        NaT           Uber *Uber *Trip  16,94
17        NaT                Mia Cookies  47,50
18        NaT           Uber *Uber *Trip  16,96
19        NaT           Uber *Uber *Trip  19,98
20        NaT           Uber *Uber *Trip  11,93
21        NaT           Uber *Uber *Trip   9,97
22        NaT           Uber *Uber *Trip   9,91
23        NaT           Uber *Uber *Trip   9,96
24        NaT              Amazonprimebr  14,90
25        NaT        Paypal *Sacocheiotv  15,00
26        NaT        Pag*Easymarketpleno   6,50

Could someone help me with this?

I'm having difficulty with Pandas when trying to convert this column to a date. The table doesn't include a year, so I think that's making the conversion difficult.

    28 JUL  Unnamed: 0        Alura *Alura - 7/12     68,00
0   28 JUL         NaN  Passei Direto S/A. - 3/12     19,90
1   31 JUL         NaN          Drogarias Pacheco     25,99
2   31 JUL         NaN     Mundo Verde - Rj - Sho      5,90
3   31 JUL         NaN              Paypal *99app      4,25
4   04 AGO         NaN            Saldo em atraso  1.091,17
5   04 AGO         NaN          Crédito de atraso  1.091,17
6   06 AGO         NaN             Apple.Com/Bill     34,90
7   07 AGO         NaN        Pagamento em 07 AGO  1.091,17
8   07 AGO         NaN            Juros de atraso     16,86
9   07 AGO         NaN              IOF de atraso      4,43
10  07 AGO         NaN            Multa de atraso     21,91
11  08 AGO         NaN             Apple.Com/Bill     21,90
12  09 AGO         NaN      Google Youtubepremium     20,90
13  10 AGO         NaN              Amazon.Com.Br     41,32
14  12 AGO         NaN           Uber *Uber *Trip     17,91
15  12 AGO         NaN           Uber *Uber *Trip     16,94
16  12 AGO         NaN                Mia Cookies     47,50
17  13 AGO         NaN           Uber *Uber *Trip     16,96
18  13 AGO         NaN           Uber *Uber *Trip     19,98
19  16 AGO         NaN           Uber *Uber *Trip     11,93
20  16 AGO         NaN           Uber *Uber *Trip      9,97
21  18 AGO         NaN           Uber *Uber *Trip      9,91
22  22 AGO         NaN           Uber *Uber *Trip      9,96
23  23 AGO         NaN              Amazonprimebr     14,90
24  27 AGO         NaN        Paypal *Sacocheiotv     15,00
25  27 AGO         NaN        Pag*Easymarketpleno      6,50

I tried to transform it using this code, but it's not working:

df["Data"] = pd.to_datetime(df["Data"], format="%d %b", errors="coerce")

Incorrect output:

Data                      Local  Valor
0  1900-07-28        Alura *Alura - 7/12  68,00
1  1900-07-28  Passei Direto S/A. - 3/12  19,90
2  1900-07-31          Drogarias Pacheco  25,99
3  1900-07-31     Mundo Verde - Rj - Sho   5,90
4  1900-07-31              Paypal *99app   4,25
7         NaT             Apple.Com/Bill  34,90
9         NaT            Juros de atraso  16,86
10        NaT              IOF de atraso   4,43
11        NaT            Multa de atraso  21,91
12        NaT             Apple.Com/Bill  21,90
13        NaT      Google Youtubepremium  20,90
14        NaT              Amazon.Com.Br  41,32
15        NaT           Uber *Uber *Trip  17,91
16        NaT           Uber *Uber *Trip  16,94
17        NaT                Mia Cookies  47,50
18        NaT           Uber *Uber *Trip  16,96
19        NaT           Uber *Uber *Trip  19,98
20        NaT           Uber *Uber *Trip  11,93
21        NaT           Uber *Uber *Trip   9,97
22        NaT           Uber *Uber *Trip   9,91
23        NaT           Uber *Uber *Trip   9,96
24        NaT              Amazonprimebr  14,90
25        NaT        Paypal *Sacocheiotv  15,00
26        NaT        Pag*Easymarketpleno   6,50

Could someone help me with this?

Share Improve this question edited 2 days ago mozway 259k13 gold badges49 silver badges93 bronze badges asked 2 days ago leotricanoleotricano 11 silver badge New contributor leotricano is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • It's just going to be a manual process. You'll probably need to change AGO to AUG at the very least, so use a mapping. As for the year, well, I just hope you know that it only spans a single year otherwise you'll have quite a bit more difficulty – roganjosh Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 4

This looks like Brazilian Portuguese, you should install the pt_BR locale on your machine, then run:

import locale
locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
df['Data_converted'] = pd.to_datetime(df['Data'], format='%d %b',
                                      errors='coerce')

Output:

     Data Data_converted
0  28 JUL     1900-07-28
1  04 AGO     1900-08-04

And, if you want to force the year:

df['Data_converted'] = pd.to_datetime('2025 ' + df['Data'],
                                      format='%Y %d %b', errors='coerce')

Output:

     Data Data_converted
0  28 JUL     2025-07-28
1  04 AGO     2025-08-04

本文标签: pythonConverting a Column to Date in PandasStack Overflow