Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: python pandas convert strings to datetime problems Date: Mon, 11 Apr 2016 20:41:27 +0200 Organization: None Lines: 37 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de 0Osk8bVPHh0wP9ID+KsakQmfWv8CR9+wEskzO8nc08hw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:problems': 0.09; 'subject:python': 0.14; '(meaning': 0.16; '00:00:00': 0.16; '1970-01-01': 0.16; 'integers.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'issue.': 0.20; 'converted': 0.22; 'seems': 0.23; 'import': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'compare': 0.27; "skip:' 10": 0.28; 'values': 0.28; 'fine': 0.28; 'code': 0.30; 'skip:d 20': 0.34; 'i.e.': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'created': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'wrong': 0.38; 'hi,': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'series': 0.65; 'compare:': 0.84; 'now).': 0.84; '1970': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd866b.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:106888 Daiyue Weng wrote: > Hi, I need to compare the years in a Series. The values in the Series is > like '1996', '2015', '2006-01-02' or '20130101' etc. The code I created > is, > > col_value_series = pd.to_datetime(col_value_series, > infer_datetime_format=True) min_year = col_value_series.min().year > max_year = col_value_series.max().year > > current_year = datetime.date.today().year > > res1 = min_year > 1970 > res2 = max_year < current_year > return min_year > 1970 and max_year < current_year > > the code is working fine on the values like '20030101' and '2006-01-02', > which are converted to datetime, i.e. '2003-01-01'. But it converted > values '1996' or '2015' to '1970-01-01 00:00:00.000001996' and '1970-01-01 > 00:00:00.000002015', which are completely wrong (meaning the years are all > 1970 now). So how to resolve the issue. This seems to happen when the year-only dates are integers. Compare: >>> import pandas as pd >>> pd.to_datetime(pd.Series(["2010-10-20", 2000])) 0 2010-10-20 00:00:00 1 1970-01-01 00:00:00.000002 dtype: datetime64[ns] >>> pd.to_datetime(pd.Series(["2010-10-20", "2000"])) 0 2010-10-20 1 2000-01-01 dtype: datetime64[ns] How is your series created? Perhaps you can ensure that you start out with strings only.