SettingWithCopyWarning при установке значения даты и времени в серии Панд - PullRequest
0 голосов
/ 14 декабря 2018

Я получаю SettingWithCopyWarning при установке значения в серии панд с помощью iloc.Я использую pandas 0.21.1 и python 3.6.7

import pandas as pd
from datetime import datetime
from pytz import timezone

tz = timezone('CET')
ambiguous_dst = True

expected_dt_series = pd.Series([
    datetime(2018, 10, 28, 1),
    datetime(2018, 10, 28, 2),
    datetime(2018, 10, 28, 3),
    datetime(2018, 10, 28, 4),
])
expected_dt_series = expected_dt_series.dt.tz_localize(
    tz, ambiguous='NaT')

expected_dt_series.iloc[1] = tz.localize(
    datetime(2018, 10, 28, 2), is_dst=ambiguous_dst) # <- line that causes error

expected_dt_series = expected_dt_series.astype('object')

output:

SettingWithCopyWarning: modifications to a method of a datetimelike object are not supported and are discarded. Change values on the original.
  self._setitem_with_indexer(indexer, value)
  x.py:17: SettingWithCopyWarning: modifications to a method of a datetimelike object are not supported and are discarded. Change values on the original.
    datetime(2018, 10, 28, 2), is_dst=ambiguous_dst)
  1. Почему это происходит?Я устанавливаю значение в исходной серии
  2. Что я могу сделать, чтобы избежать этого?(Кроме отключения предупреждения)

1 Ответ

0 голосов
/ 14 декабря 2018

именно эта часть вызывает проблему:

expected_dt_series = expected_dt_series.dt.tz_localize(tz, ambiguous='NaT')

явно говорит пандам, что это ее собственная серия, используя copy

expected_dt_series = expected_dt_series.dt.tz_localize(
    tz, ambiguous='NaT').copy()

expected_dt_series.iloc[1] = tz.localize(
    datetime(2018, 10, 28, 2), is_dst=ambiguous_dst)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...