0

I have tried the following to convert the time to Indian time for testing. But got many errors. See what I tried til now:

>>> df = pd.read_csv("Real.csv",encoding='utf-16',index_col=0)
>>> df.tail(1).index
Index(['2018.09.27 16:43:00'], dtype='object', name='Time')
>>> df.tail(1).index.tz_convert('Asia/Kolkata')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Index' object has no attribute 'tz_convert'
>>> df.index.tz_convert('Asia/Kolkata').tail(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Index' object has no attribute 'tz_convert'
>>> df.index.tz_localize('GMT').tz_convert('Asia/Kolkata').tail(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Index' object has no attribute 'tz_localize'

Here is the sample csv:

2018.09.27 16:34:00,0.94259,0.94276,0.94253,0.94276,0.002,-0.004,-0.001,-0.001,-0.007,0.001,0.002,0.001
2018.09.27 16:35:00,0.94275,0.94275,0.94249,0.94253,0.002,-0.002,-0.001,0,-0.006,0.002,0,0.001
2018.09.27 16:36:00,0.94253,0.94259,0.94235,0.94241,0,-0.003,0,0,-0.005,-0.001,0.001,0.002
2018.09.27 16:37:00,0.94241,0.94258,0.94237,0.94256,0,-0.003,0,0.001,-0.005,0,0.001,0.001
2018.09.27 16:38:00,0.94256,0.94256,0.94238,0.94245,0.001,0,0.001,0.005,0.001,0.001,-0.002,-0.001
2018.09.27 16:39:00,0.94246,0.9426,0.9423899999999999,0.94254,0.001,0,-0.001,0.003,-0.001,0.002,-0.001,0
2018.09.27 16:40:00,0.94252,0.94275,0.94252,0.94274,0.002,0.001,0,0.003,0,0.003,-0.002,-0.001
2018.09.27 16:41:00,0.94274,0.94278,0.94265,0.94268,0.001,-0.002,-0.001,0.002,-0.001,0.002,0.001,-0.001
2018.09.27 16:42:00,0.94268,0.94285,0.94268,0.94272,0,-0.001,0.001,0.002,-0.002,0.002,0,-0.001
2018.09.27 16:43:00,0.94272,0.94282,0.9426600000000001,0.9428,0.001,-0.001,0.002,0.001,-0.002,0.002,0,0

I am trying to check if the last index, which is Time, is equal to the current time as per IST. The Time in the file is of GMT format.
Kindly, help me check the condition for the Time check. Let me know what I am missing.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • is your index a DatetimeIndex ? – gyx-hh Sep 28 '18 at 09:46
  • I guess no. But is there no way I can convert only the last entry to datetime, because there is no need for the rest apart from the last entry of the datetime type. Can you help me check? – Jaffer Wilson Sep 28 '18 at 09:48

2 Answers2

1

Try using the following:

>>> df = pd.read_csv("Real.csv",encoding='utf-16',index_col=0)
>>> pd.to_datetime(df.index[-1]).to_pydatetime() == datetime.now().replace(tzinfo=None,microsecond=0,second=0)+ timedelta(hours=-2,minutes=-30)
True

It worked for me.

0

Try the following using pytz. The way i understand your question is that you have a csv with time in GMT and you want to check this time against time in India?

Since you dont want to convert the whole index to DatetimeIndex you can just retrieve the last value as suggested above by Sandeep and convert that to datetime

from datetime import datetime
from pytz import timezone

dt_gmt = datetime.strptime(df.index[-1], '%Y.%m.%d %H:%M:%S')
dt_ist = datetime.fromtimestamp(dt.timestamp(), tz=timezone('Indian/Reunion'))

and then compare dt_gmt agains dt_ist. You might have to use a different timezone name - you can find the list here Is there a list of Pytz Timezones?

gyx-hh
  • 1,421
  • 1
  • 10
  • 15