Python converting negative float64 values to dates

347
July 17, 2017, at 11:42 PM

I am having trouble I have two date columns which both come in as a float64 dtype. The file I am pulling from is a sas7bdat file and I am having trouble with some of the values primarily the values that come in as negative.

So for example my values look like such: -21914 and my positive values look like this 20797. I am using the following code right now which seems to be converting the positive values just fine but has a problem with the negative values for some reason.

df['DATE'] = (df['DATE'])
df['DATE'] = pd.to_timedelta(df['DATE'], unit='D') + pd.Timestamp('1960-1-1')
df['DATE'] =  pd.to_datetime(df['DATE'], format='%d%b%Y') 

Like I noted above this converts the positive values just fine, but the negative values it gives me the same date for each of them even though some of the negative values are different. I thought it may be a problem with the negative sign so I changed my code up and started using this.

df['DATE'] = abs(df['DATE'])
df['DATE'] = (df['DATE'])
df['DATE'] = pd.to_timedelta(df['DATE'], unit='D') + pd.Timestamp('1960-1-1')
df['DATE'] =  pd.to_datetime(df['DATE'], format='%d%b%Y') 

I turned all of the float64 values into absolute values which would turn the negative values to positive and I got a different date for the negative values which are now positive, but all of those dates are still exactly the same like they were when they were negative. I am up for suggestions at this point since I cant seem to get past this slight problem.

Answer 1

Post the output of this simple example:

import pandas as pd
import numpy as np
x = np.array([-10.0, -5.0, 0.0, 5.0, 10.0])
df = pd.DataFrame(x, index=range(0,10,2), columns=['DATE'])
df['DATE'] = (df['DATE'])
df['DATE'] = pd.to_timedelta(df['DATE'], unit='D') + pd.Timestamp('1960-1-1')
print(df['DATE'])

Should be

0   1959-12-22
2   1959-12-27
4   1960-01-01
6   1960-01-06
8   1960-01-11
Name: DATE, dtype: datetime64[ns]
READ ALSO
Using XLRD instead of CSV

Using XLRD instead of CSV

I finished editing my program so it can read a CSV file and export that data into PSSENow, I am trying to use the XLRD package for reading the excel data the same way I did it using the CSV module and I have no clue how to implement it using my example...

393
Parsing xml file in django

Parsing xml file in django

I've an xml document on my project folder and i've problem with extraction of values of an attribute of an element

461
how to use PYTHONPATH for independent python application

how to use PYTHONPATH for independent python application

Can anyone let me know how to set PYTHONPATH?

363
Odoo v10 @onchange(stage_id) doesn't work with <field name=“stage_id” widget=“statusbar” clickable=“True”/>

Odoo v10 @onchange(stage_id) doesn't work with <field name=“stage_id” widget=“statusbar” clickable=“True”/>

Odoo v10 @onchange(stage_id) doesn't work with stage_id widget fieldWhen I added field simple field in form body it worked, but not with original:

279