user3848207
user3848207

Reputation: 4927

Assign array of datetime type into panda dataframe

I have these arrays to assign into a pandata frame.

date_quote = []
price1 = []
price2 = []

The arrays have been filled with values. price1[], price2[] contains floating values while date_quote[] contains datetype values.

This is how I assign the arrays into the panda dataframe.

df = pd.DataFrame({'price1 ':price1 ,
                   'price2 ': price2 ,
                   'date': date_quote
                   })

I get the following error;

  File "pandas\_libs\tslib.pyx", line 492, in pandas._libs.tslib.array_to_datetime
  File "pandas\_libs\tslib.pyx", line 537, in pandas._libs.tslib.array_to_datetime
ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True
  File "pandas\_libs\tslibs\conversion.pyx", line 178, in pandas._libs.tslibs.conversion.datetime_to_datetime64
  File "pandas\_libs\tslibs\conversion.pyx", line 387, in pandas._libs.tslibs.conversion.convert_datetime_to_tsobject
AttributeError: 'pywintypes.datetime' object has no attribute 'nanosecond'

The problem comes from assigning date_quote[] which is datetime type. The code runs successfully if I do not assign date_quote[] into the dataframe.

Contents of date_quote[1] looks like 2018-07-26 00:00:00+00:00. I only need the date and do not need the time information in date_quote[]. Do I need to do any extra conversion to store this datetime type date_quote[] array into the dataframe?

The output of print (date_quote[:3]) is [pywintypes.datetime(2018, 7, 26, 0, 0, tzinfo=TimeZoneInfo('GMT Standard Time', True)), pywintypes.datetime(2018, 7, 27, 0, 0, tzinfo=TimeZoneInfo('GMT Standard Time', True)), pywintypes.datetime(2018, 7, 30, 0, 0, tzinfo=TimeZoneInfo('GMT Standard Time', True))]

I am using python v3.6

Upvotes: 0

Views: 1187

Answers (2)

scign
scign

Reputation: 942

You can also use the dateutil module to extract the date and time from the string representation of the pywintypes.datetime object. This way you can keep the time part too. Code below tested with Python 3.6.

import datetime, dateutil, pywintypes
today = datetime.datetime.now()  # get today's date as an example
mypydate = pywintypes.Time(today)  # create a demo pywintypes object
mydate = dateutil.parser.parse(str(mypydate))  # convert it to a python datetime
print(mydate)
print(type(mydate))

Output:

2019-05-11 12:44:03.320533
<class 'datetime.datetime'>

Upvotes: 1

user3848207
user3848207

Reputation: 4927

I found the answer to my own question. The key lies in removing the time information from date_quote[], leaving behind only the date information.

for x in range(0,int(num_elements)):
    date_quote[x] = date_quote[x].date()

The assignment works without error after the time information is removed.

Upvotes: 1

Related Questions