Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106888
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: python pandas convert strings to datetime problems |
| Date | 2016-04-11 20:41 +0200 |
| Organization | None |
| Message-ID | <mailman.34.1460400121.15650.python-list@python.org> (permalink) |
| References | <CAF_EDkvyR4Ri7R+pEWNQdEenVi6pir9zGC6ZKew2eCw93ZuJ8Q@mail.gmail.com> <negr4s$8jh$1@ger.gmane.org> |
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.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: python pandas convert strings to datetime problems Peter Otten <__peter__@web.de> - 2016-04-11 20:41 +0200
csiph-web