Jak przekonwertować ciągi w ramce danych Pandy na typ danych "date"?
Mam ramkę danych Pandy, jedna z kolumn zawiera ciągi daty w formacie YYYY-MM-DD
Dla np. '2013-10-28'
W tej chwili dtype
w kolumnie jest object
.
Jak przekonwertować wartości kolumn na format daty Pandy?
10 answers
Użyj astype
In [31]: df
Out[31]:
a time
0 1 2013-01-01
1 2 2013-01-02
2 3 2013-01-03
In [32]: df['time'] = df['time'].astype('datetime64[ns]')
In [33]: df
Out[33]:
a time
0 1 2013-01-01 00:00:00
1 2 2013-01-02 00:00:00
2 3 2013-01-03 00:00:00
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-31 08:36:33
Zasadniczo odpowiednik @ waitingkuo, ale użyłbym to_datetime
tutaj (wydaje się trochę czystszy i oferuje dodatkowe funkcjonalności np. dayfirst
):
In [11]: df
Out[11]:
a time
0 1 2013-01-01
1 2 2013-01-02
2 3 2013-01-03
In [12]: pd.to_datetime(df['time'])
Out[12]:
0 2013-01-01 00:00:00
1 2013-01-02 00:00:00
2 2013-01-03 00:00:00
Name: time, dtype: datetime64[ns]
In [13]: df['time'] = pd.to_datetime(df['time'])
In [14]: df
Out[14]:
a time
0 1 2013-01-01 00:00:00
1 2 2013-01-02 00:00:00
2 3 2013-01-03 00:00:00
Obsługa ValueError
s
Jeśli napotkasz sytuację, w której robisz
df['time'] = pd.to_datetime(df['time'])
Rzuca a
ValueError: Unknown string format
Oznacza to, że masz nieprawidłowe (nieobowiązkowe) wartości. Jeśli nie masz nic przeciwko przekonwertowaniu ich na pd.NaT
, możesz dodać argument errors='coerce'
do to_datetime
:
df['time'] = pd.to_datetime(df['time'], errors='coerce')
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-01-23 01:54:16
Wyobrażam sobie, że wiele danych trafia do pand z plików CSV, w takim przypadku można po prostu przekonwertować datę podczas początkowego odczytu CSV:
dfcsv = pd.read_csv('xyz.csv', parse_dates=[0])
gdzie 0 odnosi się do kolumny, w której znajduje się data.
Możesz również dodać , index_col=0
, jeśli chcesz, aby Data była twoim indeksem.
Zobacz https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-02-04 09:09:14
Teraz możesz zrobić df['column'].dt.date
Zauważ, że dla obiektów datetime, jeśli nie widzisz godziny, kiedy wszystkie są 00:00:00, to nie są pandy. To notatnik ipythona, który stara się, aby wszystko wyglądało ładnie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-07 00:22:59
Jeśli chcesz uzyskać format daty, a nie DATETIME:
df["id_date"] = pd.to_datetime(df["id_date"]).dt.date
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-12-06 19:50:15
Inny sposób, aby to zrobić i to działa dobrze, jeśli masz wiele kolumn do konwersji do datetime.
cols = ['date1','date2']
df[cols] = df[cols].apply(pd.to_datetime)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-04-29 14:18:46
Może się zdarzyć, że daty będą musiały być zamienione na inną częstotliwość. W takim przypadku sugerowałbym ustawienie indeksu według dat.
#set an index by dates
df.set_index(['time'], drop=True, inplace=True)
Po tym można łatwiej przekonwertować na typ formatu daty, który będzie najbardziej potrzebny. Poniżej kolejno konwertuję do wielu formatów dat, ostatecznie kończąc na zestawie dziennych dat na początku miesiąca.
#Convert to daily dates
df.index = pd.DatetimeIndex(data=df.index)
#Convert to monthly dates
df.index = df.index.to_period(freq='M')
#Convert to strings
df.index = df.index.strftime('%Y-%m')
#Convert to daily dates
df.index = pd.DatetimeIndex(data=df.index)
Dla zwięzłości nie pokazuję, że po każdej linii uruchamiam następujący kod powyżej:
print(df.index)
print(df.index.dtype)
print(type(df.index))
To daje mi następujące wyjście:
Index(['2013-01-01', '2013-01-02', '2013-01-03'], dtype='object', name='time')
object
<class 'pandas.core.indexes.base.Index'>
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03'], dtype='datetime64[ns]', name='time', freq=None)
datetime64[ns]
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>
PeriodIndex(['2013-01', '2013-01', '2013-01'], dtype='period[M]', name='time', freq='M')
period[M]
<class 'pandas.core.indexes.period.PeriodIndex'>
Index(['2013-01', '2013-01', '2013-01'], dtype='object')
object
<class 'pandas.core.indexes.base.Index'>
DatetimeIndex(['2013-01-01', '2013-01-01', '2013-01-01'], dtype='datetime64[ns]', freq=None)
datetime64[ns]
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-17 21:30:24
Spróbuj przekonwertować jeden z wierszy na znacznik czasu za pomocą funkcji PD. to_datetime, a następnie użyj .mapa do mapowania formularza do całej kolumny
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-02-13 16:59:47
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 startDay 110526 non-null object
1 endDay 110526 non-null object
import pandas as pd
df['startDay'] = pd.to_datetime(df.startDay)
df['endDay'] = pd.to_datetime(df.endDay)
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 startDay 110526 non-null datetime64[ns]
1 endDay 110526 non-null datetime64[ns]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-24 03:35:29
Dla kompletności, inną opcją, która może nie być najprostsza, nieco podobną do tej zaproponowanej przez @SSS, ale używającą raczej biblioteki datetime jest:
import datetime
df["Date"] = df["Date"].apply(lambda x: datetime.datetime.strptime(x, '%Y-%d-%m').date())
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-08-16 02:13:27